我有以下函数返回parcel的延迟:
ALTER FUNCTION [dbo].[Delay]
(@DeliveryTime int, @DeliveryHour int = 0, @Product_ID int, @Country varchar(2), @ZipCode varchar(10)
)
RETURNS int
AS
BEGIN
DECLARE @Delay int, @Guaranteed Int, @MaxDeliveryTime int, @MaxHour int, @test int
if @Country = 'FR'
begin
select top 1 @Guaranteed = pz.TProduct_Guaranteed,
@MaxDeliveryTime = pz.TProduct_delay,
@MaxHour = pz.TProduct_HourMax
from TProductZone PZ
where (pz.Country_ID = '0' OR pz.Country_ID = cast(@Country as varchar(2)))
And (Zone_ID = '0' or Zone_ID = left(@ZipCode,2))
And p.tproduct_id = @Product_ID
Order by pz.country_id desc, zone_id desc
end
Else
Begin
select top 1 @Guaranteed = pz.TProduct_Guaranteed,
@MaxDeliveryTime = pz.TProduct_delay,
@MaxHour = pz.TProduct_HourMax
from TProductZone PZ
where (pz.Country_ID = '0' OR pz.Country_ID = cast(@Country as varchar(2)))
And (Zone_ID = '0')
And p.tproduct_id = @Product_ID
Order by pz.country_id desc, zone_id desc
End
if @Guaranteed = 1
Begin
if @DeliveryTime >= @MaxDeliveryTime
Begin
set @Delay = @DeliveryTime-@MaxDeliveryTime
if @DeliveryHour > @MaxHour
begin
set @Delay = @Delay+1
end
End
else
Begin
set @Delay = 0
end
End
Else
Begin
set @Delay = 0
End
return @Delay
END
此功能用法如下:
Update TShipping
Set Delay = Delay(S.TShipping_Deliverytime, cast(left(isnull(TD.Tracking_Hour,0),2) as int), S.TProduct_ID, S.Country_Code, S.ZipCode)
From TShipping S
inner Join TrackingEndDelay TD On TD.TShipping_ID = S.TShipping_ID
WHERE IsNull(TShipping_Delivered,0) = 1
And DateDiff(day,TShipping_Date,getdate()) < 90
我想在一个独特的简单查询中转换这个函数(以及函数的调用),以减少响应时间。
这是我的查询:
Update TShipping
Set Delay =
case
WHEN TProduct_Guaranteed=0 or (TShipping_Deliverytime - TProduct_delay < 0) then 0
WHEN (TShipping_Deliverytime - TProduct_delay >= 0) and cast(left(isnull(TD.Tracking_Hour,0),2) as int) <= isnull(TProduct_HourMax,24) then TShipping_Deliverytime - TProduct_delay
when (TShipping_Deliverytime - TProduct_delay >= 0) and cast(left(isnull(TD.Tracking_Hour,0),2) as int) > isnull(TProduct_HourMax,24) then TShipping_Deliverytime - TProduct_delay +1
end
from tshipping TS
inner Join TrackingEndDelay TD On TD.TShipping_ID = TS.TShipping_ID
left join dbo.TShipping_DateInDelivery DID on ts.tshipping_id=DID.tshipping_id
outer apply (select top 1 pz.TProduct_Guaranteed, pz.TProduct_delay, pz.TProduct_HourMax
from TShipping S
inner join TProductZone PZ on S.tproduct_id = pz.tproduct_id
where (pz.Country_ID = '0' OR pz.Country_ID = S.Country_Code)
And (Zone_ID = '0' or (Zone_ID = left(S.TShipping_ZipCode,2) and S.Country_Code='FR'))
and TS.TShipping_ID=S.TShipping_ID
Order by pz.country_id desc, zone_id desc) r1
WHERE IsNull(TShipping_Delivered,0) = 1
And DateDiff(day,TShipping_Date,getdate()) < 90
问题是我增加了响应时间而不是减少响应时间。有人有另一种更好的方法来更快地完成它吗?
感谢您的帮助!!