我有以下查询返回< 300结果。它目前需要大约4秒钟才能完成,当我查看执行计划时,它表明它在集群索引扫描上花费了41%的资源。我对数据库管理的了解有限,这表明聚簇索引搜索可以提高性能。如何使查询使用聚簇索引查找而不是聚簇索引扫描?以下是相关信息和查询。
DECLARE @start date, @end date SET @start = '2013-01-01' SET @end = CAST(GETDATE() AS DATE) SELECT b.total, c.intakes, d.ships, a.CODE_, RTRIM(a.NAME_) as name, f.employee as Salesperson, g.referral_type_id, h.referral_type, e.slscode, a.city, a.STATE_, a.zip FROM PACWARE.ADS.RFDME a LEFT OUTER JOIN ( SELECT SUM(b.quantity) total, a.ref_id from event.dbo.sample a JOIN event.dbo.sample_parts b on a.id = b.sample_id JOIN PACWARE.ADS.PTDME c on b.part_id = c.CODE_ WHERE c.MEDICAREID = 'E0607' AND a.order_date between @start and @end GROUP BY a.ref_id )b on a.CODE_ = b.ref_id LEFT OUTER JOIN ( SELECT COUNT(a.CODE_)as intakes, rfcode FROM PACWARE.ADS.PMDME a WHERE a.REGDATETIME BETWEEN @start and @end GROUP BY a.RFCODE ) c on a.CODE_ = c.rfcode LEFT OUTER JOIN ( SELECT COUNT(a.CODE) as ships, b.rfcode FROM ( SELECT A.ACCOUNT AS CODE, MIN(CAST(A.BILLDATETIME AS DATE)) AS SHIPDATE FROM PACWARE.ADS.ARODME A LEFT OUTER JOIN PACWARE.ADS.PTDME B ON A.PTCODE=B.CODE_ LEFT OUTER JOIN event.dbo.newdate() D ON A.ACCOUNT=D.ACCOUNT LEFT OUTER JOIN event.dbo.newdate_extras() D2 ON A.ACCOUNT=D2.ACCOUNT WHERE A.BILLDATETIME>=@start AND A.BILLDATETIME=@start AND D.NEWDATE=@start AND D2.NEWDATE'ID' Group by A.ACCOUNT, B.MEDICAREID, A.CATEGORY ) a JOIN PACWARE.ADS.PMDME b on a.CODE = b.CODE_ GROUP BY b.RFCODE ) d on a.CODE_ = d.rfcode LEFT OUTER JOIN event.dbo.employee_slscode e on a.SLSCODE = e.slscode JOIN event.dbo.employee f on e.employee_id = f.id JOIN event.dbo.referral_data g on a.CODE_ = g.CODE_ JOIN event.dbo.referral_type h on g.referral_type_id = h.id WHERE total > 0
答案 0 :(得分:2)
我会尝试为REGDATETIME
表上的列PACWARE.ADS.PMDME
创建第一个和索引。
GO
CREATE NONCLUSTERED INDEX [IX_PMDME_REGDATETIME] ON PACWARE.ADS.PMDME
(
[REGDATETIME] ASC
)
GO
测试它是如何工作的。如果selectivity of the column足够好,我还会测试向列RFCODE
添加另一个索引(同一个表)。