简体   繁体   English

为什么非聚集索引列仍进行索引扫描而不是索引查找

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

I have the following tables 我有下表

EmployeePatientLink EmployeePatientLink

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

Patient 患者

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

PatientSchedule PatientSchedule

PatientId nvarchar(128) NULL -- nonclustered index created PatientId nvarchar(128)NULL-创建非聚集索引

and this query 和这个查询

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

I do have have similar queries like above and others perform index seek in the execution plan and Nested Loop joins. 我确实有类似上述的查询,并且其他人在执行计划和嵌套循环联接中执行索引查找。

However this one always includes Merge Join and Sort and Index scan. 但是,此操作始终包括“合并联接”,“排序和索引”扫描。

I guess I have sufficient indexes created and everything is in order and it should be doing index seek. 我想我已经创建了足够的索引,并且一切都井井有条,应该执行索引查找。

Is there a specific reason why query optimizer chooses index seek? 查询优化器选择索引查找有特定原因吗?

Try create index with included columns to avoid "key lookup" (SQL Server need search for PatientId value when EmployeeId is found) 尝试使用包含的列创建索引,以避免“键查找”(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