Indexes in SQL
An index trades write speed and disk for fast lookups on a column.
CREATE INDEX IF NOT EXISTS idx_orders_customer
ON orders (customer_id, placed_at DESC);
CREATE UNIQUE INDEX IF NOT EXISTS idx_products_sku
ON products (sku);
CREATE INDEX IF NOT EXISTS idx_orders_open
ON orders (placed_at)
WHERE status = 'pending';
How it works
- Column order in a composite index decides which filters it serves.
- A partial index covers only the rows you actually query.
EXPLAIN QUERY PLANtells you whether it is being used.
Keywords and builtins used here
CREATEDESCEXISTSIFINDEXNOTONUNIQUEWHERE
The run, in numbers
- Lines
- 9
- Characters to type
- 250
- Tokens
- 44
- Three-star pace
- 90 tpm
At the three-star pace of 90 tokens a minute, this run takes about 29 seconds.
Step 1 of 5 in Indexes, step 3 of 17 in Plans & performance.