Difference between Seek Predicate and Predicate
Difference between Seek Predicate and Predicate
Let's throw one million rows into a temp table along with a few columns:
CREATE TABLE #174860 (
PK INT NOT NULL,
COL1 INT NOT NULL,
COL2 INT NOT NULL,
PRIMARY KEY (PK)
);
INSERT INTO #174860 WITH (TABLOCK)
SELECT RN
, RN % 1000
, RN % 10000
FROM
(
SELECT TOP 1000000 ROW_NUMBER () OVER (ORDER BY (SELECT NULL)) RN
FROM master..spt_values v1,
master..spt_values v2
) t;
CREATE INDEX IX_174860_IX ON #174860 (COL1) INCLUDE (COL2);
Here I have a clustered index (by default) on the PK
column. There's a nonclustered index on COL1
that has a key column of COL1
and includes COL2
.
Consider the following query:
SELECT *
FROM #174860
WHERE PK >= 15000 AND PK < 15005
AND COL2 = 5000;
Here I'm not using BETWEEN
because Aaron Bertrand is hanging around this question.
How should SQL Server optimizer that query? Well, I know that the filter on PK
will reduce the result set to five rows. SQL server can use the clustered index to jump to those five rows instead of reading through all million rows in the table. However, the clustered index only has the PK column as a key column. Once the row is read into memory we need to apply the filter on COL2
. Here, PK
is a seek predicate and COL2
is a predicate.
SQL server finds five rows using the seek predicate and further reduces those five rows to one row with the normal predicate.
If I define the clustered index differently:
CREATE TABLE #174860 (
PK INT NOT NULL,
COL1 INT NOT NULL,
COL2 INT NOT NULL,
PRIMARY KEY (COL2, PK)
);
And run the same query I get different results:
https://www.sqlservice.se/what-is-the-difference-between-a-predicate-and-a-seek-predicate/
In this case, SQL Server can seek using both columns in the WHERE
clause. Exactly one row is read from the table using the key columns.
For one more example consider this query:
SELECT *
FROM #174860
WHERE COL1 = 500
AND COL2 = 3545;
The IX_174860_IX index is a covering index because it contains all of the columns needed for the query. However, only COL1
is a key column. SQL Server can seek with that column to find the 1000 rows with a matching COL1
value. It can further filter down those rows on the COL2
column to reduce the final result set to 0 rows.
https://www.sqlservice.se/what-is-the-difference-between-a-predicate-and-a-seek-predicate/
If you look at the Index Seek operator in a query plan of a SQL Server SELECT query, you will sometimes see that you have both a Seek Predicate and also a Predicate.
So what is the difference between these two properties of the Index seek?
Seek Predicate is the seek operation that uses the b-tree part of the index to find matching rows.
Predicate is an operation that after the Seek Predicate operation does additional filterin using non-key columns (and sometimes also on indexed columns). If this is a non clustered index it seems unintuitive to me that we would not use only Seek Predicate (except on included columns) but I have seen i happen, and my theory so far is that it might be related to statistics.
In any case, you should expect much better performance from Seek Predicate operations only Index seeks compared to Index Seeks that include both Seek Predicate and Predicate properties.
作者:Chuck Lu GitHub |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.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的安装以及使用【已经淘汰了】