簡體   English   中英

為什么非聚集索引列仍進行索引掃描而不是索引查找

[英]Why does nonclustered index column still do index scan instead of index seek

我有下表

EmployeePatientLink

Id nvarchar(128) PK & clustered index
PatientId nvarchar(128) FK NULL -- nonclustered index created
EmployeeId int NULL-- nonclustered index created. 

患者

Id nvarchar(128) PK & clustered index
Id PK & clustered Index  -- this links to above table 

PatientSchedule

PatientId nvarchar(128)NULL-創建非聚集索引

和這個查詢

select 
    Id, PatientId 
from 
    PatientSchedule ps
Inner join 
    EmployeePatientLink ep on ps.PatientId = ep.PatientId
    where ep.EmployeeId=1111 

我確實有類似上述的查詢,並且其他人在執行計划和嵌套循環聯接中執行索引查找。

但是,此操作始終包括“合並聯接”,“排序和索引”掃描。

我想我已經創建了足夠的索引,並且一切都井井有條,應該執行索引查找。

查詢優化器選擇索引查找有特定原因嗎?

嘗試使用包含的列創建索引,以避免“鍵查找”(SQL Server在找到EmployeeId時需要搜索PatientId值)

CREATE INDEX IX_EmployeePatientLink_EmployeeId ON EmployeePatientLink(EmployeeId) 
INCLUDE (PatientId)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM