Skip to content
Lab 3 of 4

Performance: Indexes, Transactions & EXPLAIN

Make a slow query fast and keep money safe. Add an index and prove the planner uses it with EXPLAIN, move funds inside a transaction, then make overdrafts impossible with a constraint.
  • Advanced
  • 45 min
  • SQL
  • Indexing
  • Transactions
  • Performance

Overview

Two things separate a hobby database from a production one: queries that stay fast as data grows, and writes that stay correct when things go wrong. This lab covers both on a table of 100,000 events.

You'll watch a lookup that scans the whole table, add an index, and use EXPLAIN to prove the planner switched to an index scan. Then you'll move money between accounts inside a transaction so the two updates succeed or fail as one, and finish by adding a CHECK constraint that makes an overdraft physically impossible to commit. Indexes, transactions, and constraints — the three pillars of a database you can trust.

What you will learn

  • Create an index to speed up a filtered lookup
  • Read an EXPLAIN plan and tell an index scan from a sequential scan
  • Move data atomically inside a BEGIN / COMMIT transaction
  • Guarantee an invariant with a CHECK constraint

Before you start

  • Joins & Aggregation first, or solid SQL query experience
  • You can run queries in psql

Tasks

  1. 01Index the hot column
  2. 02Prove it with EXPLAIN
  3. 03Move money in a transaction
  4. 04Make overdrafts impossible

Next in this course