Sub topic 13
Writing Predicates That Can Use Indexes
This chapter assumes you know basic SELECT statements, tables, columns, comparison operators, and AND and OR. You will learn how predicate shape affects index use. A predicate is a condition in a WHERE clause.
An index is an extra data structure that helps a database find rows without reading every row in a table. The database optimizer chooses whether to use an index. Writing an index-friendly predicate gives it a better option, but it does not guarantee that the index will be used.
Prefer searchable predicates
A predicate is often easier to use with an index when the database can identify a continuous section of indexed values. Simple comparisons and ranges have this shape:
SELECT id, created_at
FROM orders
WHERE created_at >= '2026-03-01'
AND created_at < '2026-04-01';If orders.created_at has an index, this half-open range can usually become an index range scan. It includes every timestamp in March without applying a function to the column.
BETWEEN is also a range condition, and it includes both endpoints:
SELECT id, name, salary
FROM employees
WHERE salary BETWEEN 50000 AND 70000;The optimizer can often use an index on salary. For timestamps, prefer an inclusive lower bound and exclusive upper bound. A condition such as created_at <= '2026-03-31' can exclude most of March 31 when the value contains a time.
LIKE and index use
A LIKE pattern can use an index when it begins with fixed text. The % wildcard then matches any sequence after that text.
SELECT id, name
FROM employees
WHERE name LIKE 'A%';An index on name may locate the section beginning with A. In contrast, these patterns usually cannot use an ordinary B-tree index efficiently:
WHERE name LIKE '%son'
WHERE name LIKE '%ann%'The leading % means the database does not know where the matching section starts. It may need to inspect many values.
The _ wildcard matches exactly one character. A pattern with fixed characters before _ may still have a usable prefix:
SELECT id, product_code
FROM products
WHERE product_code LIKE 'AB_-%';The exact behavior depends on the database, collation, and index type.
Avoid changing the indexed column
Applying a function to an indexed column can prevent an ordinary index from being used:
SELECT id, name
FROM employees
WHERE LOWER(name) LIKE 'a%';This is useful for a case-insensitive comparison, but the database must calculate LOWER(name) for many rows unless it has an expression index or a database-specific case-insensitive index. Alternatives include a case-insensitive collation, a normalized column, or an appropriate index designed for the expression.
| Predicate shape | Typical index result |
|---|---|
| `created_at >= start AND created_at < end` | Good range candidate |
| `name LIKE 'A%'` | Possible prefix scan |
| `name LIKE '%A'` | Usually broad scan |
| `LOWER(name) = 'alice'` | Needs an expression or matching functional index |
| `CAST(code AS text) = '100'` | May prevent use of an index on `code` |
Case sensitivity and collation differ across database systems. Test the actual database and index definition rather than assuming that LIKE behaves the same everywhere.
IN, OR, and selectivity
IN compares a column with several values:
SELECT id, name
FROM employees
WHERE department IN ('Sales', 'Marketing', 'Support');An index on department may support this as several point lookups. A short IN list is usually clearer than a chain of OR comparisons. Very large lists can produce a less efficient plan, so compare alternatives such as loading the values into a temporary table and joining.
Indexes are most useful when a predicate is selective, meaning it excludes many rows. An index on a column containing only two possible values may not help for a condition that matches half the table. The optimizer may correctly choose a table scan.
Practical checklist
- Keep indexed columns on one side of comparisons when possible.
- Use a fixed prefix for index-friendly
LIKEsearches. - Use half-open ranges for dates and timestamps.
- Avoid functions and casts on indexed columns unless a matching expression index exists.
- Remember that
INcan use an index, but list size and selectivity affect the plan. - Check collation, data types, and the execution plan.
Exercise
Which predicate is generally more likely to use an ordinary index on created_at: DATE(created_at) = '2026-03-01' or created_at >= '2026-03-01' AND created_at < '2026-03-02'? Why?
Show answer
The half-open range is more likely to use the index because it compares the indexed column directly. DATE(created_at) applies a function to every value unless a matching expression index exists.