Jump to content
 







Main menu
   


Navigation  



Main page
Contents
Current events
Random article
About Wikipedia
Contact us
Donate
 




Contribute  



Help
Learn to edit
Community portal
Recent changes
Upload file
 








Search  

































Create account

Log in
 









Create account
 Log in
 




Pages for logged out editors learn more  



Contributions
Talk
 



















Contents

   



(Top)
 


1 Simple example  





2 Text example  





3 See also  





4 Notes  





5 External links  





6 References  














Sargable







Add links
 









Article
Talk
 

















Read
Edit
View history
 








Tools
   


Actions  



Read
Edit
View history
 




General  



What links here
Related changes
Upload file
Special pages
Permanent link
Page information
Cite this page
Get shortened URL
Download QR code
Wikidata item
 




Print/export  



Download as PDF
Printable version
 
















Appearance
   

 






From Wikipedia, the free encyclopedia
 


Inrelational databases, a condition (or predicate) in a query is said to be sargable if the DBMS engine can take advantage of an index 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."1[1][2]

For database query optimizers, sargable is an important property in OLTP workloads because it suggests a good query plan can be obtained by a simple heuristic2 matching query to indexes instead of a complex, time-consuming cost-based search,[1] 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 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 instance 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.

Simple example

[edit]

WHERE 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, SQRT() 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

[edit]

WHERE ... LIKE clauses that are sargable have field values on the left of the operator, and LIKE 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 'Wales' 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 'Jimmy'.

See also

[edit]

Notes

[edit]
^1 Gulutzan and Pelzer, (Chapter 2, Simple "Searches")
^2 [3] gives an example of such simple heuristic.
[edit]

References

[edit]
  1. ^ a b Andy, Pavlo (Spring 2023). "CMU 15-721 :: Advanced Database Systems (Spring 2023) :: Lecture #16 Optimizer Implementation (Part 1) - Slide" (PDF). Archived (PDF) from the original on 2023-06-01. Retrieved 2024-01-25.
  • ^ Selinger, P. Griffiths; Astrahan, M. M.; Chamberlin, D. D.; Lorie, R. A.; Price, T. G. (1979). "Access Path Selection in a Relational Database Management System". SIGMOD '79: Proceedings of the 1979 ACM SIGMOD International Conference on Management of Data. ACM Press: 23. doi:10.1145/582095.582099. ISBN 978-0-89791-001-9.
  • ^ Silberschatz, Abraham; Korth, Henry F.; Sudarshan, S. (2020). Database system concepts (7th ed.). New York, NY: McGraw-Hill Education. p. 773. ISBN 978-1-260-08450-4.

  • Retrieved from "https://en.wikipedia.org/w/index.php?title=Sargable&oldid=1211033164"

    Categories: 
    Database management systems
    Relational model
    Hidden categories: 
    Articles that may contain original research from August 2015
    All articles that may contain original research
    Articles needing additional references from April 2013
    All articles needing additional references
    Articles with multiple maintenance issues
     



    This page was last edited on 29 February 2024, at 14:13 (UTC).

    Text is available under the Creative Commons Attribution-ShareAlike License 4.0; additional terms may apply. By using this site, you agree to the Terms of Use and Privacy Policy. Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.



    Privacy policy

    About Wikipedia

    Disclaimers

    Contact Wikipedia

    Code of Conduct

    Developers

    Statistics

    Cookie statement

    Mobile view



    Wikimedia Foundation
    Powered by MediaWiki