为什么这个SQL导致索引扫描而不是索引搜索?

Axe*_*eva 5 sql sql-server performance sql-execution-plan

有人可以帮我调整这个SQL查询吗?

SELECT  a.BuildingID, a.ApplicantID, a.ACH, a.Address, a.Age, a.AgentID, a.AmenityFee, a.ApartmentID, a.Applied, a.AptStatus, a.BikeLocation, a.BikeRent, a.Children, 
        a.CurrentResidence, a.Email, a.Employer, a.FamilyStatus, a.HCMembers, a.HCPayment, a.Income, a.Industry, a.Name, a.OccupancyTimeframe, a.OnSiteID,
        a.Other, a.ParkingFee, a.Pets, a.PetFee, a.Phone, a.Source, a.StorageLocation, a.StorageRent, a.TenantSigned, a.WasherDryer, a.WasherRent, a.WorkLocation, 
        a.WorkPhone, a.CreationDate, a.CreatedBy, a.LastUpdated, a.UpdatedBy
FROM    dbo.NPapplicants AS a INNER JOIN
        dbo.NPapartments AS apt ON a.BuildingID = apt.BuildingID AND a.ApartmentID = apt.ApartmentID
WHERE   (apt.Offline = 0)
AND     (apt.MA = 'M')
Run Code Online (Sandbox Code Playgroud)

.

以下是执行计划的样子:

.

在此输入图像描述

我不明白的是为什么我要为NPapplicants进行索引扫描.我有一个涵盖BuildingID和ApartmentID的索引.不应该使用吗?

Ric*_*iwi 7

这是因为期待从比赛中返回接近10K的记录.回到数据以使用10K密钥检索其他列相当于仅扫描100K记录(至少)和使用哈希匹配过滤的性能.

至于对其他表的访问,查询优化器已经确定您的索引是有用的(可能是针对Offline或MA),因此它正在寻找该索引以获取连接键.

然后将这两个HASH匹配用于交叉点以产生最终输出.