What makes a SQL statement sargable?

Sql ServerPerformance

Sql Server Problem Overview


By definition (at least from what I've seen) sargable means that a query is capable of having the query engine optimize the execution plan that the query uses. I've tried looking up the answers, but there doesn't seem to be a lot on the subject matter. So the question is, what does or doesn't make an SQL query sargable? Any documentation would be greatly appreciated.

For reference: Sargable

Sql Server Solutions


Solution 1 - Sql Server

The most common thing that will make a query non-sargable is to include a field inside a function in the where clause:

SELECT ... FROM ...
WHERE Year(myDate) = 2008

The SQL optimizer can't use an index on myDate, even if one exists. It will literally have to evaluate this function for every row of the table. Much better to use:

WHERE myDate >= '01-01-2008' AND myDate < '01-01-2009'

Some other examples:

Bad: Select ... WHERE isNull(FullName,'Ed Jones') = 'Ed Jones'
Fixed: Select ... WHERE ((FullName = 'Ed Jones') OR (FullName IS NULL))

Bad: Select ... WHERE SUBSTRING(DealerName,4) = 'Ford'
Fixed: Select ... WHERE DealerName Like 'Ford%'

Bad: Select ... WHERE DateDiff(mm,OrderDate,GetDate()) >= 30
Fixed: Select ... WHERE OrderDate < DateAdd(mm,-30,GetDate()) 

Solution 2 - Sql Server

Don't do this:

WHERE Field LIKE '%blah%'

That causes a table/index scan, because the LIKE value begins with a wildcard character.

Don't do this:

WHERE FUNCTION(Field) = 'BLAH'

That causes a table/index scan.

The database server will have to evaluate FUNCTION() against every row in the table and then compare it to 'BLAH'.

If possible, do it in reverse:

WHERE Field = INVERSE_FUNCTION('BLAH')

This will run INVERSE_FUNCTION() against the parameter once and will still allow use of the index.

Solution 3 - Sql Server

In this answer I assume the database has sufficient covering indexes. There are enough questions about [this topic][1].

A lot of the times the sargability of a query is determined by the tipping point of the related indexes. The tipping point defines the difference between seeking and scanning an index while joining one table or result set onto another. One seek is of course much faster than scanning a whole table, but when you have to seek a lot of rows, a scan could make more sense.

So among other things a SQL statement is more sargable when the optimizer expects the number of resulting rows of one table to be less than the tipping point of a possible index on the next table.

You can find a detailed post and example [here][2].

[1]: https://stackoverflow.com/search?q=database+indexing "Database indexing" [2]: http://www.sqlskills.com/BLOGS/KIMBERLY/post/Why-arent-those-nonclustered-indexes-being-used.aspx

Solution 4 - Sql Server

For an operation to be considered sargable, it is not sufficient for it to just be able to use an existing index. In the example above, adding a function call against an indexed column in the where clause, would still most likely take some advantage of the defined index. It will "scan" aka retrieve all values from that column (index) and then eliminate the ones that do not match to the filter value provided. It is still not efficient enough for tables with high number of rows. What really defines sargability is the query ability to traverse the b-tree index using the binary search method that relies on half-set elimination for the sorted items array. In SQL, it would be displayed on the execution plan as a "index seek".

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDForck42View Question on Stackoverflow
Solution 1 - Sql ServerBradCView Answer on Stackoverflow
Solution 2 - Sql ServerbeachView Answer on Stackoverflow
Solution 3 - Sql ServerDries Van HansewijckView Answer on Stackoverflow
Solution 4 - Sql Serveruser2011845View Answer on Stackoverflow