17
In database queries what is the concept of a sargable argument (Search Argument Able)?
17
In database queries what is the concept of a sargable argument (Search Argument Able)?
14
I know this question is almost a year old but I think that maybe the answer might interest someone.
Briefly a predicate or condition is considered sargable (Search Argument Able) if DBMS can take advantage of an index to improve the execution time of a particular query.
A typical situation that makes a query non-sargable is to include in the WHERE clause a function which has as a parameter a column. For example:
SELECT *
FROM tbl_tabela
WHERE YEAR(ColunaData) = 2008
In this situation it is not possible to use an index on the column Colunadata, even if it exists. This function will therefore be evaluated for each record in the table. An alternative to use would be:
SELECT *
FROM tbl_tabela
WHERE ColunaData >= '01-01-2008' AND ColunaData < '01-01-2009'
Another example that is often used:
SELECT *
FROM tbl_tabela
WHERE Field LIKE '%blah%'
Or else:
SELECT *
FROM tbl_tabela
WHERE SUBSTRING(ColumnVarchar, 1, 6) = 'Joaquim'
Alternatively it could be used:
SELECT *
FROM tbl_tabela
WHERE ColumnVarchar LIKE 'Joaquim%'
For final example I leave the treatment of NULL which we encounter almost daily
How not to:
SELECT *
FROM tbl_Tabela
WHERE ISNULL(ColunaNome, 'Manuel Joaquim') = 'Manuel Joaquim'
Correct version:
SELECT *
FROM tbl_tabela
WHERE ColunaNome= 'Manuel Joaquim'
OR ColunaNome IS NULL
A final note. It is noteworthy that the concept of sargability applies not only to the WHERE clause but also to ORDER BY, GROUP BY and HAVING. SELECT can contain expressions non-sargable that this has no impact on performance.
Only as a complement, predicates sargable also affect the junctions in the FROM/ON clause. In the article "Building Efficient T-SQL Code: Sargability" the subject is dealt with in detail. Log in: https://portosql.wordpress.com/2018/10/04/construindo-codigos-t-sql-eficientes-sargability/
Browser other questions tagged database query terminology
You are not signed in. Login or sign up in order to post.
Did you find a solution? Poste as an answer to help other people.
– Maniero
@bigown, not yet, maybe I need to do a good search and summarize in an answer. The question is a little 'weak'? da para entender?
– rray
I think so, she doesn’t seem too complicated :) I think you just need to have someone who knows and wants to answer.
– Maniero
@rray In addition to Bruno’s reply, here is a suggested article in which the subject is dealt with in detail: https://portosql.wordpress.com/2018/10/04/construindo-codigos-t-sql-efficiency/
– José Diz