backendgym backendgym — reps Sign in with GitHubSign in
← Reps
PostgreSQL Advanced

SQL · Optimizing

Read a query plan and say why it is slow; add the index that changes it, and see what two concurrent sessions do to a balance.

sql-optimizing / README.md

bgym start sql-optimizing brings up a pinned Postgres 16 container and generates 500,000 deterministic movie rows in the seed. Nothing is installed on your machine. Write SQL in reps/NN/answer.sql; bgym run NN sends it to psql. Rep 8 has its own run.sh and a two-session fixture. bgym reset removes the volume and regenerates the data.

You should have finished sql-viewing and be comfortable reading a query result. This set assumes you can look up plan and transaction syntax. The work is to read what the engine chose, what an index costs, and what two connections can do between one check and the next.

The movie table is large enough for a selective title lookup to change from a sequential scan to an index scan. The seed also contains two similarly named movies, two wallet balances and one seat, so a broad predicate, a half-transaction or a lost update is visible. The row generator is a script in seed/02-data.sql, not a checked-in data dump.

Do sql-viewing before this set. Do sql-scaling-and-security after it.

Reps

In order — each one assumes the ones before it. Open a rep to read it.

  1. 01 01 · Read the first plan
  2. 02 02 · Make the lookup selective
  3. 03 03 · Judge an over-broad lookup
  4. 04 04 · Shape advanced indexes
  5. 05 05 · Measure index write cost
  6. 06 06 · Judge vacuum timing
  7. 07 07 · Make a transfer atomic
  8. 08 08 · Reproduce a lost update