โ† Backend Performance & Scaling learning path
Backend Performance & Scaling ยท Module 3 of 8

Database query performance

Most slow backends are slow databases.

๐Ÿ“– Key ideas

  1. An index lets the database find rows without scanning the whole table, like a book's index.
  2. Use EXPLAIN (or the query plan) to see whether a query uses an index or scans the table.
  3. The N+1 problem: one query for a list plus one query per item; fetch in one joined or batched query instead.
  4. Select only the columns you need and paginate large results.
  5. Use a connection pool; opening a new database connection for every request is slow.
๐Ÿ’ก Real-life example: Searching a table without an index is like finding one student's marks by reading every page of the register instead of using the roll-number index.
IndexStructure that speeds up finding rows
Query planHow the database will run a query
N+1 problemOne query plus one more query per item
Connection poolReused set of open database connections