What does it mean for a query to be 'SARGable', and why does using a function like WHERE YEAR(date) = 2024 often prevent index usage?
Last updated
A SARGable (Search ARGument-able) predicate leaves the indexed column bare on one side so the optimizer can seek the index directly.
The index stores
date, notYEAR(date), so the function must be evaluated per row (a full scan). Leading wildcards, implicit conversions, and arithmetic on the column break SARGability the same way.Fix by rewriting to a half-open range (
>= '2024-01-01' AND < '2025-01-01') or by adding a functional index on the expression.
- https://medium.com/@pinjarimustafa27/understanding-sargable-and-non-sargable-queries-in-sql-server-085efaddac79
- https://en.wikipedia.org/wiki/Sargable
- https://jntech.in/view/what-are-sargable-and-non-sargable-conditions
- https://www.brentozar.com/archive/2018/03/cant-index-probably-not-sargable/
- https://www.sqlshack.com/how-to-use-sargable-expressions-in-t-sql-queries-performance-advantages-and-examples/
- https://mariadb.com/docs/server/ha-and-performance/optimization-and-tuning/query-optimizations/sargable-date-and-year
- https://medium.com/@NusratGulbarga_data_analyst/15-advanced-sql-interview-questions-with-solutions-df649e18978a
- https://medium.com/@sohahashim/optimizing-sql-queries-using-sargable-queries-9ed6dd31db00