Sargable

https://en.wikipedia.org/wiki/Sargable

In relational 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

 

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.

  • Sargable operators: =, >, <, >=, <=, BETWEEN, LIKE, IS [NOT] NULL
  • Sargable operators that rarely improve performance: <>, IN, OR, NOT IN, NOT LIKE

Simple example

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  11.7 < SQRT(myIntField)

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. 

 

作者:Chuck Lu    GitHub    
posted @   ChuckLu  阅读(109)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
历史上的今天:
2020-05-28 To Allow App through Windows Defender Firewall in Command Prompt
2019-05-28 Activator.CreateInstance with parameters
2019-05-28 outlook使用inline style回复邮件
2019-05-28 回复git@vger.kernel.org的注意事项
2019-05-28 Majordomo Info VGER.KERNEL.ORG
2018-05-28 how to modify vs2017
2018-05-28 node inspector的安装以及使用【已经淘汰了】
点击右上角即可分享
微信分享提示