How it sneaks in
An admin screen lists 50 bookings and shows each customer's name. The code fetches the bookings, then loops and fetches each customer by id. It works fine in development with five rows and becomes painfully slow with real data. ORMs hide the extra queries behind innocent-looking property access, which is why the problem is so common.
Finding it
Turn on query logging in a test run, or use a profiler such as Laravel Debugbar, Django Debug Toolbar or an APM tool. A page that issues dozens of near-identical queries differing only by an id is the signature.
Fixes
- Eager loading: ask the ORM to load related rows together (include, with, select_related, prefetch_related).
- A join: fetch bookings and customers in one SQL statement.
- Batching: collect the ids and load them with a single IN query. Add a test that fails when a screen exceeds an expected query count.
Questions, answered
Every query costs a network round trip and database work, and those costs add up across hundreds of calls.
Yes, in resolvers. Libraries such as DataLoader batch the lookups to avoid it.