我有一个存储过程,它引用多个表(四个具体来说,即RefurbRef,ActivationDetailRefurb,ActivationDetailReplaced,ReplacedData),每个表上大约有1lac的数据。
我需要将存储过程中的数据绑定到前端的UI。当我尝试在我的SQL Server2008上执行存储过程时,执行和获取结果花了将近20分钟。用户不可能等待长时间盯着“请等待加载”用户界面。
步骤如下:
CREATE procedure [dbo].[uspLotFailureDetail]
@fromDate varchar(50),
@toDate varchar(50),
@vendorName varchar(50),
@modelName varchar(50)
AS
BEGIN
select
d.LOTQty,
ApprovedQty = count(distinct d.SerialNUMBER),
d.DispatchDate,
Installed = count(a.SerialNumber) + count(r.SerialNumber),
DOA = sum(case when datediff(day, coalesce(a.ActivationDate,r.ActivationDate), f.RecordDate) between 0 and 10 then 1 else 0 end),
Bounce = sum(case when datediff(day, coalesce(a.ActivationDate,r.ActivationDate), f.RecordDate) between 11 and 180 then 1 else 0 end)
from
RefurbRef d
left join
ActivationDetailRefurb a on d.SerialNUMBER= a.SerialNumber
and d.DispatchDate <= a.ActivationDate
and d.LOTQty = a.LOTQty
left join
ActivationDetailReplaced r on d.SerialNUMBER= r.SerialNumber
and d.DispatchDate <= r.ActivationDate
and d.LOTQty = r.LotQty
and (a.ActivationDate is null or a.ActivationDate <= d.DispatchDate)
left join
ReplacedData f on f.OldSerialNumber = (coalesce (a.SerialNumber, r.SerialNumber))
and f.RecordDate >= (coalesce (a.ActivationDate, r.ActivationDate))
where
d.DispatchDate between @fromDate and @toDate
and d.VendorName = @vendorName
and d.Model = @modelName
group by
d.LOTQty, d.DispatchDate
END该过程提取的结果有两种类型,基于供应商的结果和基于模型的结果。然而,如果结果是基于供应商提取的,即仅使用@fromDate、@toDate和@Vendor,则过程执行并获得结果只需不到2分钟。但是,当像上面的过程一样使用所有四个变量时,执行所需的时间不少于20分钟。
有没有什么方法可以优化查询来提高过程的性能?
提前感谢
发布于 2015-08-30 11:15:27
根据所提供的信息,我将查看RefurbRef.model编号,以查看此字段上是否有覆盖索引。我敢打赌,一旦你添加了这个标准,它就不会跳到20分钟。此外,我会将变量更改为varchar中的日期。
@fromDate Date,
@toDate Date,希望这能帮上忙,杰森
https://stackoverflow.com/questions/32196931
复制相似问题