我的查詢生成一些關於加速,上次和平均速度的報告。 這是我的查詢:查詢優化 - 花費太長時間並停止服務器
Select
r1 . *, r2.name, r2.notes, r2.serial
From
(SELECT
k.idgps_unit,
MIN(k.dt) AS DT_Start,
MIN(CASE
WHEN k.RowNumber = 1 THEN k.Lat
END) AS Latitude_Start,
MIN(CASE
WHEN k.RowNumber = 1 THEN k.Long
END) AS Longitude_Start,
MIN(CASE
WHEN k.RowNumber = 1 THEN k.Speed_kmh
END) AS Speed_Start,
MAX(k.dt) AS dt_end,
MIN(CASE
WHEN k.RowNumber = MaxRowNo THEN k.Lat
END) AS Latitude_End,
MIN(CASE
WHEN k.RowNumber = MaxRowNo THEN k.Long
END) AS Longitude_End,
MIN(CASE
WHEN k.RowNumber = MaxRowNo THEN k.Speed_kmh
END) AS Speed_End,
AVG(Speed_kmh) AS Average_Speed
FROM
(SELECT
gps_unit_location . *,
@i:=CASE
WHEN Speed_Kmh > 80 AND @b = 0 THEN @i + 1
ELSE @i
END AS IntervalID,
@r:=CASE
WHEN Speed_Kmh > 80 AND @b = 0 THEN 1
ELSE @r + 1
END AS RowNumber,
@b:=CASE
WHEN Speed_Kmh > 80 THEN 1
ELSE 0
END AS IntervalCheck
FROM
gps_unit_location, (SELECT @i:=0) i, (SELECT @r:=0) r, (SELECT @b:=0) b
ORDER BY dt , idgps_unit_location) k
INNER JOIN (SELECT
IntervalID, MAX(RowNumber) AS MaxRowNo
FROM
(SELECT
gps_unit_location . *,
@i:=CASE
WHEN Speed_Kmh > 80 AND @b = 0 THEN @i + 1
ELSE @i
END AS IntervalID,
@r:=CASE
WHEN Speed_Kmh > 80 AND @b = 0 THEN 1
ELSE @r + 1
END AS RowNumber,
@b:=CASE
WHEN Speed_Kmh > 80 THEN 1
ELSE 0
END AS IntervalCheck
FROM
gps_unit_location, (SELECT @i:=0) i, (SELECT @r:=0) r, (SELECT @b:=0) b
ORDER BY dt , idgps_unit_location) d
WHERE
IntervalCheck = 1
GROUP BY IntervalID) MaxInt ON MaxInt.IntervalID = k.IntervalID
WHERE
k.IntervalCheck = 1
and k.idgps_unit in (SELECT
idgps_unit
FROM
instafleet.gps_unit
where
id_customer = (select
idcustomer
from
user
where
iduser = 14))
GROUP BY k.IntervalID , k.idgps_unit) r1
Inner join
gps_unit r2 ON r1.idgps_unit = r2.idgps_unit
目前,它需要3分鐘783723條記錄。我認爲適當的指標可能會有所幫助;雖然經過一些試驗和錯誤,但我無法弄清楚。如果你認爲你可以提供幫助,並需要一些額外的信息 - 我會很高興你提供給你。
解釋
結果
這是一個很長的查詢別人來優化您的免費重寫。你可以把它分解成更小的查詢來隔離問題嗎? – 2013-03-12 17:17:31
Marcus,我認爲添加合適的索引可以解決它的性能問題..不需要重做它...我可能是錯的:) – Andrew 2013-03-12 17:20:39
您正在使用哪種GUI軟件? – dynamic 2013-03-20 18:33:47