Stop 11 · Indexes & Query PerformanceTopics · 7/9

SARGable Queries

Part of the SQL Roadmap

In short

You can have the perfect index and still get a full table scan, just because of how you wrote the WHERE clause. SARGable filters are the ones an index can actually use.

A SARGable condition can use an index. Wrapping an indexed column in a function, like WHERE YEAR(order_date) = 2025, usually stops the index being used.

The jaadu flips a fat dictionary open to exactly the right section in one move, while behind it a second, tired jaadu is buried under open dictionaries, reading page after page.

Think of it like this

Words that start with, words that end with

In a dictionary, finding every word that starts with 'pra' is quick: flip to P, then to 'pra', and read one block. Finding every word that ends with 'ing' means reading the whole dictionary, because it is sorted by the start of words. An index is sorted by the raw column value. Ask about the value directly and it jumps. Change the value first and it has to read everything.

Watch it happen

Same question, two ways to ask

Not SARGable

  • WHERE EXTRACT(YEAR FROM order_date) = 2025
  • Function runs on every row first
  • Index on order_date is ignored
  • Seq Scan on the whole table

SARGable

  • WHERE order_date >= '2025-01-01' AND order_date < '2026-01-01'
  • Bare column compared to constants
  • Index jumps to the start of 2025
  • Reads only that year's slice

Keep the indexed column alone on one side of the comparison. Move any maths or function to the constant side.

The big ideas

What you need to know

No functions on the column

UPPER(name), DATE(created_at) or YEAR(order_date) on an indexed column usually hides it from the index.

No maths on the column

WHERE amount * 1.18 > 1180 can't use an index. WHERE amount > 1000 can.

Leading wildcards

LIKE 'Ra%' can use an index. LIKE '%ra' can't, because the start of the value is unknown.

Expression indexes

If you must filter on LOWER(email), create an index on LOWER(email) itself.

See it in code

Walk through the code

sql
1-- Assume: CREATE INDEX idx_orders_date ON orders (order_date);2 3-- Not SARGable: EXTRACT runs on every row4SELECT COUNT(*) FROM orders5WHERE EXTRACT(YEAR FROM order_date) = 2025;6 7-- SARGable: a plain range on the bare column8SELECT COUNT(*) FROM orders9WHERE order_date >= DATE '2025-01-01'10  AND order_date <  DATE '2026-01-01';11 12-- Not SARGable: leading wildcard13SELECT * FROM customers WHERE phone LIKE '%9876';14 15-- Fix a function filter with an expression index16CREATE INDEX idx_customers_lower_email ON customers (LOWER(email));17SELECT * FROM customers WHERE LOWER(email) = 'riya.menon@example.com';

1/4Looks neat, but the database has to compute the year for every row before comparing.

MySQL has the same problem with YEAR(order_date) and supports functional indexes from 8.0.13. In PostgreSQL, LIKE 'abc%' uses a normal B-tree index only with the C collation or a text_pattern_ops index.

Save yourself the pain

Common mistakes

Oops

WHERE DATE(created_at) = '2026-11-20' on an indexed timestamp.

Do this instead

WHERE created_at >= '2026-11-20' AND created_at < '2026-11-21'. Same rows, and the index works.

Oops

WHERE CAST(customer_id AS TEXT) = '301' because the input arrived as text.

Do this instead

Convert the input, not the column: WHERE customer_id = 301.

Your mission

Rewrite and measure

~20 min

Quick check

Which filter can use a plain index on order_date?

Go deeper

  1. 1Rewrite a YEAR() filter as a date range with >= and <.
  2. 2Avoid leading wildcards like LIKE '%abc' on indexed columns.
  3. 3Check the plan with EXPLAIN before and after the rewrite.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge