Sargable predicates: why an index is ignored and how to rewrite the filter
An index can only help when the predicate gives it something to seek to. WHERE created_at >= '2026-01-01' gives the B-tree a starting point. WHERE date(created_at) = '2026-01-01' wraps the column in a function, so the database must compute the function for every row and cannot use the plain index. The first is sargable; the second is not.
The same applies to arithmetic, casts, concatenation and pattern matching. The rule of thumb is to keep the indexed column alone on one side of the comparison. Move the transformation to the constant or the parameter: created_at >= '2026-01-01' AND created_at < '2026-01-02' instead of a date function. The range form is both indexable and easier for the planner to estimate.
| Non-sargable | Sargable rewrite |
|---|---|
| date(created_at) = '2026-01-01' | created_at >= '2026-01-01' AND created_at < '2026-01-02' |
| lower(email) = 'a@b.c' | Expression index on lower(email), or store normalised email |
| id + 0 = 42 | id = 42 |
| name LIKE '%son' | Trigram index, or a reverse index for suffix search |
| cast(id as text) = '42' | id = 42, with matching types |
| substring(code, 1, 3) = 'ABC' | Expression index, or a separate prefix column |
OR and NOT IN
An OR across different columns can prevent a single index from being used. The planner may choose a bitmap OR of two indexes, which works but is less efficient than one ordered index. Rewriting as a UNION of two indexed queries often produces a better plan, at the cost of two scans and a deduplication step. Test both before choosing.
NOT IN with a subquery has a second problem: if the subquery returns a NULL, the comparison is unknown for every row and the query returns nothing. NOT EXISTS is safer and often faster, because it can stop at the first match and does not need to materialise the list. The same reasoning applies to NOT IN with a list that may contain NULL.
Expression indexes and generated columns
When the query genuinely needs a function, index the expression: CREATE INDEX idx_users_lower_email ON users (lower(email));. The planner matches the query's expression with the index's expression, so the same function must be written the same way, with the same immutability. An expression index is a good fit for case-insensitive lookups, normalised codes and extracted fields.
A generated column stores the computed value and can be indexed like any other column. It is easier for other queries to use and for people to understand, at the cost of storage and a write-time computation. PostgreSQL supports stored generated columns; MySQL supports both virtual and stored. Choose based on whether the value is used in several queries and whether the function is immutable.
Pattern matching
A leading wildcard, LIKE '%term', cannot use a B-tree because the pattern has no fixed prefix. A trailing wildcard, LIKE 'term%', can, because the prefix is known. For substring search, PostgreSQL's pg_trgm extension provides GIN or GiST indexes that support %term%, and full-text search is better for word matching with ranking. Neither is a drop-in replacement; each changes the semantics of the match.
Case-insensitive matching has the same shape. ILIKE '%term%' is hard to index; a trigram index on the lowercased column, combined with a lowercased pattern, can work. The plan shows whether the index is used or whether the filter is applied after a scan.
Checking the rewrite
- Look at the plan: is the predicate shown as an Index Cond or as a Filter after a scan?
- If it is a Filter on a sequential scan, check for a function, cast, arithmetic or leading wildcard.
- Rewrite the predicate to keep the column alone, or add an expression index.
- Run EXPLAIN ANALYZE again and confirm the node changed to an index scan.
- Compare the actual time and buffers, not just the plan shape.
The analyzer flags a function on a filtered column and a leading wildcard, because both usually mean the index cannot be used. The flags are heuristics: a function on a tiny table or a leading wildcard on a few hundred rows may be fine. The plan's actual rows and time tell you whether the rewrite is worth it.
Keep the column alone. If you must transform it, transform the constant instead, or index the expression.
Sources
- PostgreSQL documentation — Expression indexes
- PostgreSQL documentation — pg_trgm
- MySQL documentation — Generated columns
Questions
What does sargable mean?
A predicate is sargable if the database can use an index to evaluate it, typically because the indexed column appears alone on one side of a comparison. A function call, a cast or arithmetic on the column usually makes it non-sargable, so the index cannot seek and the plan falls back to a scan.
Is LIKE '%term%' always unindexable?
With a standard B-tree, yes, because the pattern has no fixed prefix to seek to. PostgreSQL extensions such as pg_trgm support trigram indexes for substring search, and full-text search is designed for word queries. For a suffix search on a small set, a scan may be acceptable.
Do implicit casts really break indexes?
They can. If the column is text and the parameter is an integer, or the column is numeric and the literal is text, the comparison may cast the column and lose the index. Make the types match explicitly, or index the cast expression. The plan showing a filter instead of an index condition is the clue.
The analyzer flags a function on a filtered column and a leading wildcard in a filter.
Open the analyzerWritten against the PostgreSQL and MySQL documentation and the standard cost model; every plan in the examples can be pasted into the analyzer linked above.
Back to the guides