wtf( )unctionsystem design, drawn
← all problemsDatabasesMedium

One query, then two hundred more

The orders page loads a page of 200 orders, then loops over them asking for each order's customer. That's 201 queries — each one fast, each one a round trip, and the page takes eight seconds.

The database isn't struggling. It is answering 201 trivial questions that should have been one.

Replace the per-row lookups with a single request.
Components — tap one, then tap a slot on the diagram
!Loading one page issues two hundred and one separate queries.

Outside every boundary: Orders page, App, Database (answering 201 times; FAILED: 201 queries), an empty slot for the on the query path Connections: Orders page calls App (step 1) App calls on the query path — 1 query, not 201 (step 2) on the query path calls Database (step 3)

Orders page
App
Databaseanswering 201 times201 queries