SQL Performance Lab
This lab is a workbench: one table with 100,000 payments, and a set of experiments to run on it. Each experiment shows a query's real plan, adds an index or rewrites the query, and shows the plan again, so you can see exactly how many rows each version reads. Change anything you like in Try it: drop an index, swap column orders, add WHERE conditions, and compare. Every run starts from a fresh table, so you can't break anything.
The Table
Every experiment starts by building the same table: 100,000 payments for 5,000 students over two years. It takes about a second.
CREATE TABLE big_payments (
payment_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
student_id INTEGER NOT NULL, paid_on DATE NOT NULL,
amount_usd NUMERIC(8,2) NOT NULL, method TEXT, note TEXT
);
INSERT INTO big_payments (student_id, paid_on, amount_usd, method, note)
SELECT 1000 + (g % 5000), DATE '2024-01-01' + (g % 730), 100 + (g % 400),
(ARRAY['card', 'check', 'cash'])[1 + g % 3], 'payment ' || g
FROM generate_series(1, 100000) AS g;
ANALYZE big_payments;How to read the plans: look at the top node and its actual rows, then for Rows Removed by Filter (rows read and thrown away) and the scan type: Seq Scan reads the whole table, Index Scan and Bitmap Heap Scan read only matching rows, Index Only Scan doesn't touch the table at all (Reading EXPLAIN).
Experiment 1: One Student's Payments
Goal: Find every payment of student 1234.
CREATE TABLE big_payments (
payment_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
student_id INTEGER NOT NULL, paid_on DATE NOT NULL,
amount_usd NUMERIC(8,2) NOT NULL, method TEXT, note TEXT
);
INSERT INTO big_payments (student_id, paid_on, amount_usd, method, note)
SELECT 1000 + (g % 5000), DATE '2024-01-01' + (g % 730), 100 + (g % 400),
(ARRAY['card', 'check', 'cash'])[1 + g % 3], 'payment ' || g
FROM generate_series(1, 100000) AS g;
ANALYZE big_payments;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT * FROM big_payments WHERE student_id = 1234;
CREATE INDEX big_payments_student_idx ON big_payments (student_id);
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT * FROM big_payments WHERE student_id = 1234;CREATE TABLE
INSERT 0 100000
ANALYZE
QUERY PLAN
------------------------------------------------------
Seq Scan on big_payments (actual rows=20.00 loops=1)
Filter: (student_id = 1234)
Rows Removed by Filter: 99980
(3 rows)
CREATE INDEX
QUERY PLAN
---------------------------------------------------------------------------------
Bitmap Heap Scan on big_payments (actual rows=20.00 loops=1)
Recheck Cond: (student_id = 1234)
Heap Blocks: exact=20
-> Bitmap Index Scan on big_payments_student_idx (actual rows=20.00 loops=1)
Index Cond: (student_id = 1234)
Index Searches: 1
(6 rows)Before the index, a sequential scan reads all 100,000 rows to return 20. After it, a bitmap index scan finds those 20 directly. See Indexes.
Experiment 2: A Date Range
Goal: Total collected in the first week of 2025.
CREATE TABLE big_payments (
payment_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
student_id INTEGER NOT NULL, paid_on DATE NOT NULL,
amount_usd NUMERIC(8,2) NOT NULL, method TEXT, note TEXT
);
INSERT INTO big_payments (student_id, paid_on, amount_usd, method, note)
SELECT 1000 + (g % 5000), DATE '2024-01-01' + (g % 730), 100 + (g % 400),
(ARRAY['card', 'check', 'cash'])[1 + g % 3], 'payment ' || g
FROM generate_series(1, 100000) AS g;
ANALYZE big_payments;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT sum(amount_usd) FROM big_payments
WHERE paid_on >= '2025-01-01' AND paid_on < '2025-01-08';
CREATE INDEX big_payments_paid_idx ON big_payments (paid_on);
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT sum(amount_usd) FROM big_payments
WHERE paid_on >= '2025-01-01' AND paid_on < '2025-01-08';CREATE TABLE
INSERT 0 100000
ANALYZE
QUERY PLAN
--------------------------------------------------------------------------------------
Aggregate (actual rows=1.00 loops=1)
-> Seq Scan on big_payments (actual rows=959.00 loops=1)
Filter: ((paid_on >= '2025-01-01'::date) AND (paid_on < '2025-01-08'::date))
Rows Removed by Filter: 99041
(4 rows)
CREATE INDEX
QUERY PLAN
------------------------------------------------------------------------------------------------
Aggregate (actual rows=1.00 loops=1)
-> Bitmap Heap Scan on big_payments (actual rows=959.00 loops=1)
Recheck Cond: ((paid_on >= '2025-01-01'::date) AND (paid_on < '2025-01-08'::date))
Heap Blocks: exact=148
-> Bitmap Index Scan on big_payments_paid_idx (actual rows=959.00 loops=1)
Index Cond: ((paid_on >= '2025-01-01'::date) AND (paid_on < '2025-01-08'::date))
Index Searches: 1
(7 rows)A range on the bare column can use the index. Try rewriting the filter as EXTRACT(WEEK FROM paid_on) = 1 and watch the index disappear from the plan. See Index Design.
Experiment 3: Latest Ten
Goal: The ten most recent payments.
CREATE TABLE big_payments (
payment_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
student_id INTEGER NOT NULL, paid_on DATE NOT NULL,
amount_usd NUMERIC(8,2) NOT NULL, method TEXT, note TEXT
);
INSERT INTO big_payments (student_id, paid_on, amount_usd, method, note)
SELECT 1000 + (g % 5000), DATE '2024-01-01' + (g % 730), 100 + (g % 400),
(ARRAY['card', 'check', 'cash'])[1 + g % 3], 'payment ' || g
FROM generate_series(1, 100000) AS g;
ANALYZE big_payments;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT payment_id, paid_on FROM big_payments ORDER BY paid_on DESC, payment_id DESC LIMIT 10;
CREATE INDEX big_payments_paid_id_idx ON big_payments (paid_on DESC, payment_id DESC);
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT payment_id, paid_on FROM big_payments ORDER BY paid_on DESC, payment_id DESC LIMIT 10;CREATE TABLE
INSERT 0 100000
ANALYZE
QUERY PLAN
----------------------------------------------------------------------
Limit (actual rows=10.00 loops=1)
-> Sort (actual rows=10.00 loops=1)
Sort Key: paid_on DESC, payment_id DESC
Sort Method: top-N heapsort Memory: 17kB
-> Seq Scan on big_payments (actual rows=100000.00 loops=1)
(5 rows)
CREATE INDEX
QUERY PLAN
--------------------------------------------------------------------------------------------------
Limit (actual rows=10.00 loops=1)
-> Index Only Scan using big_payments_paid_id_idx on big_payments (actual rows=10.00 loops=1)
Heap Fetches: 10
Index Searches: 1
(4 rows)Without a matching index, PostgreSQL sorts all 100,000 rows to keep ten (a top-N heapsort). An index in the same order lets it read the first ten entries and stop. See Pagination.
Experiment 4: Deep Pages
Goal: Page 500 of a list, 20 per page: OFFSET versus keyset.
CREATE TABLE big_payments (
payment_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
student_id INTEGER NOT NULL, paid_on DATE NOT NULL,
amount_usd NUMERIC(8,2) NOT NULL, method TEXT, note TEXT
);
INSERT INTO big_payments (student_id, paid_on, amount_usd, method, note)
SELECT 1000 + (g % 5000), DATE '2024-01-01' + (g % 730), 100 + (g % 400),
(ARRAY['card', 'check', 'cash'])[1 + g % 3], 'payment ' || g
FROM generate_series(1, 100000) AS g;
ANALYZE big_payments;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT payment_id FROM big_payments ORDER BY payment_id LIMIT 20 OFFSET 9980;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT payment_id FROM big_payments WHERE payment_id > 9980 ORDER BY payment_id LIMIT 20;CREATE TABLE
INSERT 0 100000
ANALYZE
QUERY PLAN
----------------------------------------------------------------------------------------------
Limit (actual rows=20.00 loops=1)
-> Index Only Scan using big_payments_pkey on big_payments (actual rows=10000.00 loops=1)
Heap Fetches: 10000
Index Searches: 1
(4 rows)
QUERY PLAN
-------------------------------------------------------------------------------------------
Limit (actual rows=20.00 loops=1)
-> Index Only Scan using big_payments_pkey on big_payments (actual rows=20.00 loops=1)
Index Cond: (payment_id > 9980)
Heap Fetches: 20
Index Searches: 1
(5 rows)OFFSET still reads and throws away the 9,980 earlier rows; the keyset version starts right after the last row seen. See Pagination.
Experiment 5: Covering Index
Goal: Count card payments per student for one student, reading only the index.
CREATE TABLE big_payments (
payment_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
student_id INTEGER NOT NULL, paid_on DATE NOT NULL,
amount_usd NUMERIC(8,2) NOT NULL, method TEXT, note TEXT
);
INSERT INTO big_payments (student_id, paid_on, amount_usd, method, note)
SELECT 1000 + (g % 5000), DATE '2024-01-01' + (g % 730), 100 + (g % 400),
(ARRAY['card', 'check', 'cash'])[1 + g % 3], 'payment ' || g
FROM generate_series(1, 100000) AS g;
ANALYZE big_payments;
CREATE INDEX big_payments_student_method_idx ON big_payments (student_id, method);
VACUUM big_payments;
EXPLAIN (ANALYZE, COSTS OFF, TIMING OFF, SUMMARY OFF, BUFFERS OFF)
SELECT count(*) FROM big_payments WHERE student_id = 1234 AND method = 'card';CREATE TABLE
INSERT 0 100000
ANALYZE
CREATE INDEX
VACUUM
QUERY PLAN
--------------------------------------------------------------------------------------------------------
Aggregate (actual rows=1.00 loops=1)
-> Index Only Scan using big_payments_student_method_idx on big_payments (actual rows=7.00 loops=1)
Index Cond: ((student_id = 1234) AND (method = 'card'::text))
Heap Fetches: 0
Index Searches: 1
(5 rows)All the columns the query needs are in the index, and VACUUM has marked the pages all-visible, so an index-only scan answers without touching the table. See Index Design.
Your Own Experiments
Try these on the same table, and predict each plan before you run it:
| Try | What to watch |
|---|---|
An index on (paid_on, student_id) instead of (student_id, paid_on) for experiment 1 | Whether the planner still uses it for student_id = 1234 alone |
WHERE note LIKE 'payment 12%' with and without CREATE INDEX ON big_payments (note text_pattern_ops) | Prefix searches can use a pattern index |
WHERE method = 'card' with an index on method | A third of the table matches, so a sequential scan may still win |
SET enable_seqscan = off before a query | What the planner would do if it had to use an index, and why it didn't want to |
Was this tutorial helpful?