SQL Query Performance
Why some queries are fast and others aren't — and how to find out which is which before it matters.
What & Why
A query's speed depends on how much data it has to touch, whether it can skip straight to relevant rows, and how the database chooses to execute the statement you wrote. This lesson series covers reading that execution choice (EXPLAIN), speeding it up (indexes), and writing queries that give the planner better options in the first place.
See How It Works
BUSINESS QUESTION
Inspect only recent signup fields instead of scanning and returning every user column.
| id | created_at | country | channel | plan | activated_at | churned_at |
|---|---|---|---|---|---|---|
| 101 | 2024-01-15 09:10:00+00 | US | organic | pro | 2024-01-16 14:25:00+00 | NULL |
| 102 | 2024-02-10 11:05:00+00 | CA | paid | free | NULL | 2024-03-20 10:00:00+00 |
| 103 | 2024-03-12 16:35:00+00 | GB | referral | pro | 2024-03-13 08:15:00+00 | NULL |
| 104 | 2024-04-01 13:20:00+00 | US | organic | free | 2024-04-03 12:00:00+00 | NULL |
EXAMPLE QUERY
SELECT
id,
channel,
plan,
created_at
FROM growth.users
WHERE created_at >= TIMESTAMPTZ '2024-03-01 00:00:00+00'
ORDER BY created_at DESC, id
LIMIT 100;RESULT — users created on or after March 1
| id | channel | plan | created_at |
|---|---|---|---|
| 104 | organic | free | 2024-04-01 13:20:00+00 |
| 103 | referral | pro | 2024-03-12 16:35:00+00 |
The fixed timestamp boundary makes this result deterministic and the descending order returns the newest qualifying user first.
This lesson's practice is part of Pro.
Sign up free to try it on a real business scenario