我有一个用SQL编写的查询工作正常,但我需要优化查询
这是我的问题:
ALTER PROCEDURE Connection4_Alt @StartLocation VARCHAR(MAX),@EndLocation VARCHAR(MAX)
AS
BEGIN
WITH T1 AS(
select Distinct t1.Train_No as Train_1,t1.Train_Name as Train_Name1,t2.Train_No as Train_2,t2.Train_Name as Train_Name2,t3.Train_No as Train_3,t3.Train_Name as Train_Name3,t4.Train_No as Train_4,t4.Train_Name as Train_Name4,
((Select Max(t4.Distance) as MaxDistance From Rail t4 where t4.Train_No=t1.Train_No) - t1.Distance) as Distance_1, (Select Max(t4.Distance) From Rail t4 where t4.Train_No=t2.Train_No) as Distance_2 ,(Select Max(t4.Distance) From Rail t4 where t4.Train_No=t3.Train_No) as Distance_3,t4.Distance as Distance_4,
t1.Source_Station_Name_1 as Source_Station,
(Select Top 1 Last_Value(t4.Station_Name) over (Partition by T4.Train_No Order BY t4.islno ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) from Rail t4 where t4.Train_No=T1.Train_No) as Connecting_Station1,
(Select Top 1 Last_Value(t4.Station_Name) over (Partition by T4.Train_No Order BY t4.islno ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) from Rail t4 where t4.Train_No=T2.Train_No) as Connecting_Station2,
(Select Top 1 LAST_Value(t4.Station_Name) over (Partition by T4.Train_No Order BY t4.islno ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING) from Rail t4 where t4.Train_No=T3.Train_No) as Connecting_Station3,
t4.Destination_Station_Name_1 as Destination_Station,
(Select Top 1 Convert(time,Last_Value(t5.Depature_time) over (Partition by T5.Train_No Order BY t5.islno ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) from Rail t5 where t5.Train_No=T1.Train_No) as DP_T1L,
(Select Top 1 Convert(time,First_Value(t5.Depature_time) over (Partition by T5.Train_No Order BY t5.islno ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) from Rail t5 where t5.Train_No=T2.Train_No) as DP_T2F,
(Select Top 1 Convert(time,Last_Value(t5.Depature_time) over (Partition by T5.Train_No Order BY t5.islno ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) from Rail t5 where t5.Train_No=T2.Train_No) as DP_T2L,
(Select Top 1 Convert(time,First_Value(t5.Depature_time) over (Partition by T5.Train_No Order BY t5.islno ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) from Rail t5 where t5.Train_No=T3.Train_No) as DP_T3F,
(Select Top 1 Convert(time,Last_Value(t5.Depature_time) over (Partition by T5.Train_No Order BY t5.islno ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) from Rail t5 where t5.Train_No=T3.Train_No) as DP_T3L,
(Select Top 1 Convert(time,First_Value(t5.Depature_time) over (Partition by T5.Train_No Order BY t5.islno ASC ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING)) from Rail t5 where t5.Train_No=T4.Train_No) as DP_T4F,
CASE
WHEN t1.Train_Name LIKE '%Exp' and t1.Train_Name NOT LIKE '%SF Exp%' THEN 50
WHEN t1.Train_Name Like '%Pass%' THEN 33
WHEN t1.Train_Name LIKE '%SF Exp%' THEN 60
ELSE 50 END as AvgSpeed_1,
CASE
WHEN t2.Train_Name LIKE '%Exp' and t2.Train_Name NOT LIKE '%SF Exp%' THEN 50
WHEN t2.Train_Name Like '%Pass%' THEN 33
WHEN t2.Train_Name LIKE '%SF Exp%' THEN 60
ELSE 50 END as AvgSpeed_2,
CASE
WHEN t3.Train_Name LIKE '%Exp' and t3.Train_Name NOT LIKE '%SF Exp%' THEN 50
WHEN t3.Train_Name Like '%Pass%' THEN 33
WHEN t3.Train_Name LIKE '%SF Exp%' THEN 60
ELSE 50 END as AvgSpeed_3,
CASE
WHEN t4.Train_Name LIKE '%Exp' and t4.Train_Name NOT LIKE '%SF Exp%' THEN 50
WHEN t4.Train_Name Like '%Pass%' THEN 33
WHEN t4.Train_Name LIKE '%SF Exp%' THEN 60
ELSE 50 END as AvgSpeed_4
from Rail t1
join Rail t2 on (t2.Source_Station_Name=t1.Destination_Station_Name)
join Rail t3 on (t3.Source_Station_Name=t2.Destination_Station_Name)
JOIN RAIL t4 on (t4.Source_Station_Name=t3.Destination_Station_Name)
where t1.Source_Station_Name_1 Like @StartLocation
and t4.Destination_Station_Name_1 Like @EndLocation
),
T2 AS (
SELECT Train_1,Train_Name1,Train_2,Train_Name2,Train_3,Train_Name3,Train_4,Train_Name4 ,Source_Station,Connecting_Station1,Connecting_Station2,Connecting_Station3,Destination_Station,(Distance_1+Distance_2+Distance_3+Distance_4) as TotalDistance,
CASE
WHEN DP_T2F < DP_T1L
THEN DATEDIFF(HH,DP_T1L,DP_T2F)+24
ELSE DATEDIFF(HH,DP_T1L,DP_T2F)
END as Waiting_Time_1,
CASE
WHEN DP_T3F < DP_T2L
THEN DATEDIFF(HH,DP_T2L,DP_T3F)+24
ELSE DATEDIFF(HH,DP_T2L,DP_T3F)
END as Waiting_Time_2,
CASE
WHEN DP_T4F < DP_T3L
THEN DATEDIFF(HH,DP_T3L,DP_T4F)+24
ELSE DATEDIFF(HH,DP_T3L,DP_T4F)
END as Waiting_Time_3,
CAST(Distance_1/AvgSpeed_1 as decimal(18,2)) as Travelling_Time_1,
CAST(Distance_2/AvgSpeed_2 as decimal(18,2)) as Travelling_Time_2,
CAST(Distance_3/AvgSpeed_3 as decimal(18,2)) as Travelling_Time_3,
CAST(Distance_4/AvgSpeed_4 as decimal(18,2)) as Travelling_Time_4
FROM T1
)
SELECT TOP 1 (Train_1+'-'+Train_2+'-'+Train_3+'-'+Train_4)as Train_No,(LTRIM(RTRIM(Source_Station))+'-'+LTRIM(RTRIM(Connecting_Station1))+'-'+LTRIM(RTRIM(Connecting_Station2))+'-'+LTRIM(RTRIM(Connecting_Station3))+'-'+LTRIM(RTRIM(Destination_Station)))as Train_Route,TotalDistance,(Waiting_Time_1+Waiting_Time_2+Waiting_Time_3) as Waiting_Time,(Travelling_Time_1+Travelling_Time_2+Travelling_Time_3+Travelling_Time_4) as Travelling_Time,Source_Station,Destination_Station FROM T2
ORDER BY TotalDistance,Travelling_Time
END
我也按照需要获得输出,但执行时间更长,所以如何优化它。