HOME

TheInfoList



OR:

In
relational databases A relational database is a (most commonly digital) database based on the relational model of data, as proposed by E. F. Codd in 1970. A system used to maintain relational databases is a relational database management system (RDBMS). Many relation ...
, a condition (or predicate) in a query is said to be sargable if the
DBMS In computing, a database is an organized collection of data stored and accessed electronically. Small databases can be stored on a file system, while large databases are hosted on computer clusters or cloud storage. The design of databases spa ...
engine can take advantage of an
index Index (or its plural form indices) may refer to: Arts, entertainment, and media Fictional entities * Index (''A Certain Magical Index''), a character in the light novel series ''A Certain Magical Index'' * The Index, an item on a Halo megastru ...
to speed up the execution of the query. The term is derived from a contraction of ''Search ARGument ABLE''. It was first used by IBM researchers as a contraction of Search ARGument, and has come to mean simply "can be looked up by an index." A query failing to be sargable is known as a non-sargable query and typically has a negative effect on query time, so one of the steps in
query optimization Query optimization is a feature of many relational database management systems and other databases such as NoSQL and graph databases. The query optimizer attempts to determine the most efficient way to execute a given query by considering the pos ...
is to convert them to be sargable. The effect is similar to searching for a specific term in a book that has no index, beginning at page one each time, instead of jumping to a list of specific pages identified in an index. The typical situation that will make a
SQL query The SQL SELECT statement returns a result set of records, from one or more tables. A SELECT statement retrieves zero or more rows from one or more database tables or database views. In most applications, SELECT is the most commonly used data man ...
non-sargable is to include in the WHERE clause a function operating on a column value. The WHERE clause is not the only clause where sargability can matter; it can also have an effect on ORDER BY, GROUP BY and HAVING clauses. The SELECT clause, on the other hand, can contain non-sargable expressions without adversely affecting the performance. Some database management systems, for instanc
PostgreSQL, support functional indices
Conceptually, an index is simply a mapping between a value and one or more locations. With a functional index, the value stored in the index is the output of the function specified when the index is created. This capability expands what is sargable beyond base column expressions. * Sargable operators: * Sargable operators that rarely improve performance:


Simple example

clauses that are sargable typically have field values on the left of the operator, and scalar values or expressions on the right side of the operator. Not sargable: SELECT * FROM myTable WHERE SQRT(myIntField) > 11.7 This is ''not sargable'' because myIntField is embedded in a function. If any indexes were available on myIntField, they could not be used. In addition, would be called on every record in myTable. Sargable version: SELECT * FROM myTable WHERE myIntField > 11.7 * 11.7 This is sargable because myIntField is NOT contained in a function, making any available indexes on myIntField potentially usable. Furthermore, the expression is evaluated only once, rather than for each record in the table.


Text example

... clauses that are sargable have field values on the left of the operator, and text strings that do not begin with the on the right. Not sargable: SELECT * FROM myTable WHERE myNameField LIKE '%Wales%' -- Begins with %, not sargable This is ''not'' sargable. It must examine every row to find the fields containing the substring in any position. Sargable version: SELECT * FROM myTable WHERE myNameField LIKE 'Jimmy%' -- Does not begin with %, sargable This is sargable. It can use an index to find all the myNameField values that start with the substring .


See also

*
Block Range Index A Block Range Index or BRIN is a database indexing technique. They are intended to improve performance with extremely large tables. BRIN indexes provide similar benefits to horizontal partitioning or sharding but without needing to explicitly decla ...
*
Query optimization Query optimization is a feature of many relational database management systems and other databases such as NoSQL and graph databases. The query optimizer attempts to determine the most efficient way to execute a given query by considering the pos ...


Notes

: Gulutzan and Pelzer,
Chapter 2, ''Simple "Searches"''


References

* ''SQL Performance Tuning'' by Peter Gulutzan, Trudy Pelzer (Addison Wesley, 2002)
Chapter 2, ''Simple "Searches"''
* ''Microsoft SQL Server 2012 Internals'' by Kalen Delaney, Connor Cunningham, Jonathan Kehayias, Benjamin Nevarez, Paul S. Randal (O'Reily, 2013) {{ISBN, 978-0-7356-5856-1 (Chapter 11, The Query Optimizer)


External links


SQL Shack - How to use sargable expressions in T-SQL queries; performance advantages and examples

DBA.StackExchange.com - What does the word “SARGable” really mean?
Database management systems Relational model