我有以下表格
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
我确实有类似上面的查询,其他人在执行计划和嵌套循环连接中执行索引查找。
然而,这一个总是包括合并加入和排序和索引扫描。
我想我已经创建了足够的索引,一切都井然有序,它应该是索引搜索。
查询优化器选择索引查找是否有特定原因?
答案 0 :(得分:0)
尝试使用包含的列创建索引以避免"键查找" (当找到EmployeeId时,SQL Server需要搜索PatientId值)
CREATE INDEX IX_EmployeePatientLink_EmployeeId ON EmployeePatientLink(EmployeeId)
INCLUDE (PatientId)