为什么非聚簇索引列仍然执行索引扫描而不是索引查找

时间:2017-04-06 12:27:56

标签: sql sql-server sql-execution-plan

我有以下表格

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 

我确实有类似上面的查询,其他人在执行计划和嵌套循环连接中执行索引查找。

然而,这一个总是包括合并加入和排序和索引扫描。

我想我已经创建了足够的索引,一切都井然有序,它应该是索引搜索。

查询优化器选择索引查找是否有特定原因?

1 个答案:

答案 0 :(得分:0)

尝试使用包含的列创建索引以避免"键查找" (当找到EmployeeId时,SQL Server需要搜索PatientId值)

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