Sql-Server
即使存在覆蓋索引,也會對分區表進行聚集索引掃描
我有一個基於 col1 int 分區的分區表。我也有一個覆蓋索引,用於我試圖排除故障的查詢。
https://www.brentozar.com/pastetheplan/?id=BkNrNdgHm
以上是方案
任由 SQL Server 決定對整個表進行聚集索引掃描,這顯然很慢。如果我強制索引(如上面的計劃),查詢會快速執行。
SQL Server 使用什麼神奇的邏輯來確定被覆蓋的索引沒有用?我不確定 top/orderby 和 rowgoal 是否與它有關。
我的表結構是
Create table object2(col1 int, col3 datetime, col4 int, col5, col6 etc) clusterd on col1 nonclustered non aligned index is on col3,col4 (col1 is clustered so its included in nonclust) SELECT top(?) Object1.Column1 FROM Object2 Object1 WITH (NOLOCK,index(Column2)) WHERE Object1.Column3 >= ? AND Object1.Column4 IN (?) ORDER BY Object1.Column1
編輯添加的回購
CREATE PARTITION FUNCTION [PFtest](int) AS RANGE RIGHT FOR VALUES (100000, 200000, 300000, 400000, 500000, 600000, 700000, 800000, 900000, 1000000) GO CREATE PARTITION SCHEME [PStest] AS PARTITION [PFtest] all TO ([PRIMARY]); GO create table test([ID] [int] IDENTITY(1,1) NOT NULL primary key clustered,[Created] [datetime] NULL,[Type] [int] NULL,text1 varchar(10),text2 varchar(20)) on pstest(id) set nocount on declare @a int =1 declare @type int while 1=1 begin if @a%30 =0 insert into test (Created, Type, text1, text2) select getdate(),4,'four','four' else insert into test (Created, Type, text1, text2) select getdate(),1,'one','one' set @a=@a+1 end create nonclustered index ncl1 on test(created, type) select min(created),max(created) from test --2018-08-02 22:46:40.187 2018-08-02 22:49:01.577 SELECT top(10) ID FROM test WHERE Created >= '2018-08-02 22:49:01' AND Type IN (1, 4) ORDER BY ID -- clustered index scan SELECT top(10) ID FROM test WHERE Created >= '2018-08-02 22:49:01.577' AND Type IN (1, 4) ORDER BY ID-- index seek of ncl1
是的,這很可能是行目標問題。
SQL Server 評估掃描聚集索引(以避免排序)
?
將比在非聚集索引中搜尋匹配項、排序然後返回頂部匹配項更快地找到第一個匹配項(此時掃描將停止)?
。
OPTION (QUERYTRACEON 4138)
在您的 SQL Server 版本上,您可以通過執行查詢以禁用行目標來測試設置行目標是否是原因。相關問答: