Sub topic 8
Writing Predicates an Index Can Use
Assume you know SELECT, WHERE, ORDER BY, tables, and primary keys. You should also understand that a linear scan examines rows one by one, while an index can narrow the search.
What makes a predicate index-friendly?
A predicate is the condition in a WHERE clause. An index-friendly predicate, often called sargable, lets the database compare an indexed column directly with a value or range.
For example, suppose orders.created_at has a B-tree index:
CREATE INDEX orders_created_at_idx
ON orders (created_at);
-- Index-friendly range search
SELECT count(*)
FROM orders
WHERE created_at >= '2026-01-01'
AND created_at < '2026-02-01';The database can seek to the first January timestamp and then scan the matching part of the index.
A common mistake is to apply a function to the indexed column:
-- Usually prevents a plain created_at index from being used efficiently
SELECT count(*)
FROM orders
WHERE date_trunc('month', created_at) = '2026-01-01';This asks the database to calculate date_trunc for rows before it can compare the result. Rewrite the condition as a range instead.
| Predicate form | Typical result with an index on `created_at` |
|---|---|
| `created_at >= start AND created_at < end` | Efficient range search |
| `date(created_at) = target_date` | Often requires scanning many rows |
| `created_at + interval '1 day' < cutoff` | Often prevents a direct seek |
Keep the indexed column bare
The same rule applies to arithmetic, casts, and string operations:
-- Prefer a range
SELECT *
FROM orders
WHERE created_at >= '2026-03-01'
AND created_at < '2026-04-01';
-- Often less index-friendly
SELECT *
FROM orders
WHERE created_at::date = '2026-03-01';A comparison against a transformed value is usually better:
SELECT *
FROM orders
WHERE customer_id = 42;Implicit type conversion can also interfere with index use, depending on the database and data types. Compare compatible types explicitly, and avoid converting the column inside the predicate.
An expression can still be indexed deliberately. If users search without regard to letter case, an expression index matches the expression in the query:
CREATE INDEX users_lower_email_idx
ON users (lower(email));
SELECT *
FROM users
WHERE lower(email) = '[email protected]';The query must use the indexed expression closely enough for the optimizer to recognize it.
Verify instead of guessing
Use EXPLAIN to inspect the selected plan:
EXPLAIN
SELECT *
FROM orders
WHERE created_at >= '2026-01-01'
AND created_at < '2026-02-01';Look for an Index Scan, Index Only Scan, or sometimes a Bitmap Index Scan. A Seq Scan means the database chose to read the table sequentially. That choice is not automatically wrong: if the range matches a large part of the table, scanning may be cheaper than fetching many scattered rows through the index.
Exercise
Rewrite SELECT * FROM orders WHERE date_trunc('day', created_at) = '2026-05-10'; so a plain B-tree index on created_at can support a range search.
Show answer
Use SELECT * FROM orders WHERE created_at >= '2026-05-10' AND created_at < '2026-05-11';