Query Execution & OptimizationdebuggingBeginner

The list endpoint is slow but every query is fast

Symptoms

  • GET /users takes 900 ms to return 100 users.
  • Every query in the slow-query log is sub-millisecond.
  • The database log shows the same SELECT repeated 100 times with different ids.
-- database log, in one request:
SELECT id, name FROM users LIMIT 100;
SELECT count(*) FROM orders WHERE user_id = 1;
SELECT count(*) FROM orders WHERE user_id = 2;
SELECT count(*) FROM orders WHERE user_id = 3;
... (97 more)

Investigate

Inspect areas in any order (0/4 inspected). When you think you know the root cause, commit to it.

The database slow-query log
The full statement log for one request
Application code rendering the list
Network latency to the database