In
relational databases
A relational database (RDB) is a database based on the relational model of data, as proposed by E. F. Codd in 1970.
A Relational Database Management System (RDBMS) is a type of database management system that stores data in a structured form ...
, 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 or a type of data store based on the use of a database management system (DBMS), the software that interacts with end users, applications, and the database itself to capture and ana ...
engine can take advantage of an
index
Index (: indexes or 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 the Halo Array in the ...
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
International Business Machines Corporation (using the trademark IBM), nicknamed Big Blue, is an American Multinational corporation, multinational technology company headquartered in Armonk, New York, and present in over 175 countries. It is ...
researchers as a contraction of Search ARGument, and has come to mean simply "can be looked up by an index."
For database
query optimizers, sargable is an important property in
OLTP
Online transaction processing (OLTP) is a type of database system used in transaction-oriented applications, such as many operational systems. "Online" refers to the fact that such systems are expected to respond to user requests and process them i ...
workloads because it suggests a good
query plan A query plan (or query execution plan) is a sequence of steps used to access data in a SQL relational database management system. This is a specific case of the relational model concept of access plans.
Since SQL is declarative, there are typical ...
can be obtained by a simple
heuristic
A heuristic or heuristic technique (''problem solving'', '' mental shortcut'', ''rule of thumb'') is any approach to problem solving that employs a pragmatic method that is not fully optimized, perfected, or rationalized, but is nevertheless ...
matching query to indexes instead of a complex, time-consuming cost-based search,
thus it is often desired to write sargable queries. 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 po ...
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 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 decl ...
*
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 po ...
Notes
: Gulutzan and Pelzer,
Chapter 2, ''Simple "Searches"''
:
gives an example of such simple heuristic.
External links
SQL Shack - How to use sargable expressions in T-SQL queries; performance advantages and examplesDBA.StackExchange.com - What does the word “SARGable” really mean?
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)
Database management systems
Relational model