简单SQL Server查询耗时太长



此查询耗时太长,无法产生结果。

表大约有1.5亿行,查询没有缺失索引。

select 
Number, BankContacts_ID
from
dbo.BankContactNumbers b with (nolock)
where 
b.BankContacts_ID = 1234
order by 
b.ID
offset 0 rows fetch next 10 rows only

表结构如下:

create table BankContactNumbers
(
ID int identity
constraint PK_BankContactNumbers primary key nonclustered
with (fillfactor = 70),
BankContacts_ID  int not null,
Number           char(11)
)
create index IX_BankContactNumbers_BankContacts_ID
on BankContactNumbers (BankContacts_ID) include (ID, Number)

执行计划为:https://www.brentozar.com/pastetheplan/?id=SJ8S1TiLo

BankContacts_ID上已有的索引对等式谓词有用,但对ORDER BY没用,因为包含了ID列而不是键列。

修改现有索引,添加ID作为第二个键列。这样,可以将BankContacts_ID用于WHERE条件,将ID序列返回的行用于ORDER BY,而无需在查询计划中进行排序。此外,包含的Number列将允许非聚集索引覆盖查询。

CREATE INDEX IX_BankContactNumbers_BankContacts_ID ON BankContactNumbers (BankContacts_ID, ID) include (Number) WITH(DROP_EXISTING=ON);

最新更新