Sargable, short for Search ARGument ABLE, is a definition in relational database management systems for queries that can take advantage of indexes to speed up the query. A query that fails this definition is known as a non-sargable query and usually has a negative effect on query performance. So, simply making a query sargable CAN increase its performance alone, but in concert with the right indexes the results can be dramatic.
A common non-sargable query is one that includes a function call in the WHERE
clause. Below I’ll go through some concrete examples. However, sargability is not limited to just the where clause. It can effect the ORDER BY
, GROUP BY
and HAVING
clauses.
The SELECT
clause is the only one that can contain non-sargable expressions without negative effects on performance.
NOTE
Please note that these are general rules and sargability can depend on the type of database you use.
Sargable Expressions Advantages
- consuming less system resources
- speeding up query performance
- using indexes more effectively
Sargable Expressions Examples
Sargable operators: =
, >
, <
, >=
, <=
, BETWEEN
, LIKE
, IS [NOT] NULL
, EXISTS
Sargable operators that rarely improve performance: <>
, IN
, OR
, NOT IN
, NOT EXISTS
, NOT LIKE
Not including field in a function
Bad
The query optimizer can’t use an index on my_date
because it will have to evaluate the function year for every row causing a table scan. A sargable approach would be to use BETWEEN
or >=
and <=
.
Good
Other common examples:
LIKE value not beginning with a wildcard
When using the LIKE
operator, if you use a wildcard value at the beginning it causes a table scan.
Bad
Good