The Scenario
An API endpoint `GET /articles` fetches the latest 50 articles. For each article, the ORM runs a separate query to fetch the author's name. This results in 51 database queries for a single API request.
The Brief
Identify the N+1 query in the provided code snippet. Rewrite the database logic to use a SQL `JOIN` or your ORM's "eager loading" feature so that it only takes a maximum of 2 queries to fetch all the data.
Deliverables
- The optimized code snippet
- An explanation of why the N+1 problem causes severe performance degradation as traffic scales
Submission Guidance
If using Prisma, look into `include`. If using TypeORM, look into `relations`. If using raw SQL, write a `LEFT JOIN`.
Submit Your Work
Your submission is graded against the rubric on the right. If you pass, you get a public Badge URL you can share on LinkedIn. There is no draft save, so work offline first and paste your finished response here.