繁体   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