他の人の助けを借りて、テーブル内のレコード間のマイレージ (+ または -) の差を計算するこの SQL ステートメントがあります。問題は、計算された値に基準 (>0) を追加すると、クエリが劇的に遅くなることです。他のフィールドに基準を追加すると、期待どおりに動作します (長い遅延はありません)。
SELECT T1.Date,
T1.Route,
T1.BookingID,
T1.StreetNumber,
T1.Street,
T1.Arrive,
T1.Perform,
T1.Miles,
T1.Miles - (SELECT Miles
FROM Test1 AS T2
WHERE T2.Route = T1.Route
AND T2.IDNumber = (SELECT Min(IDNumber)
FROM Test1 AS T3
WHERE T3.Route = T1.Route
AND T3.IDNumber >
T1.IDNumber)) AS
Difference
FROM Test1 AS T1
GROUP BY T1.Date,
T1.Route,
T1.BookingID,
T1.StreetNumber,
T1.Street,
T1.Arrive,
T1.Perform,
T1.Miles,
T1.IdNumber,
T1.Status,
T1.Activityy
HAVING (( ( [T1].[Miles] - (SELECT Miles
FROM Test1 AS T2
WHERE T2.Route = T1.Route
AND T2.IDNumber = (SELECT Min(IDNumber)
FROM Test1 AS T3
WHERE T3.Route = T1.Route
AND T3.IDNumber >
T1.IDNumber)) ) > 0 ))
ORDER BY T1.IdNumber;