2011-08-29 190 views
0

激起許多事情值得嘗試優化一個MySQL查詢連接7臺時?MySQL查詢優化

select trips.tripid as tripid 
    , stops.stopdescrption as "perron" 
    , DATE_FORMAT(segments.segmentstart, "%H:%i") as "time" 
    , DATE_FORMAT(trips.tripend, "%H:%i") as "arrival" 
    , UPPER(routes.routepublicidentifier) as "lijn" 
    , plcend.placedescrption as "destination" 
from calendar 
       join trips on calendar.vsid=trips.vsid 
       join routes on routes.routeid=trips.routeid 
       join places plcstart on plcstart.placeid=trips.placeidstart 
       join places plcend on plcend.placeid=trips.placeidend 
       join segments on segments.tripid = trips.tripid 
       join stops on segments.stopid = stops.stopid 
where (stops.stopid = :perrons0 
    OR stops.stopid = :perrons1 OR stops.stopid = :perrons2 
    OR stops.stopid = :perrons3 OR stops.stopid = :perrons4 
    OR stops.stopid = :perrons5 OR stops.stopid = :perrons6 
    OR stops.stopid = :perrons7 OR stops.stopid = :perrons8 
    OR stops.stopid = :perrons9 OR stops.stopid = :perrons10 
    OR stops.stopid = :perrons11 OR stops.stopid = :perrons12 
    OR stops.stopid = :perrons13 OR stops.stopid = :perrons14 
    ) 
    AND calendar.vscdate = DATE(DATE_ADD(now(), INTERVAL "07:00" HOUR_MINUTE)) 
    AND segments.segmentstart >= TIME(DATE_ADD(now(), INTERVAL "07:00" HOUR_MINUTE)) 
    AND routes.routeservicetype = 0 
    AND segments.segmentstart > "00:00:00" 
ORDER BY segments.segmentstart 

有查詢,我似乎無法想到的任何事情去改變那將優化這...這超時....

任何提示的歡迎!

+1

你有什麼指標? –

+0

'EXPLAIN'計劃展示了什麼? –

回答

1

怎麼樣使用IN關鍵字,而不是多個OR的。此外,你不需要指定AND segments.segmentstart > "00:00:00"因爲你已經提供了條件segments.segmentstart >= TIME(DATE_ADD(now(), INTERVAL "07:00" HOUR_MINUTE))是大於"00:00:00"。最後,索引你的密鑰是優化執行的好主意。

select trips.tripid as tripid 
, stops.stopdescrption as "perron" 
, DATE_FORMAT(segments.segmentstart, "%H:%i") as "time" 
, DATE_FORMAT(trips.tripend, "%H:%i") as "arrival" 
, UPPER(routes.routepublicidentifier) as "lijn" 
, plcend.placedescrption as "destination" 
from calendar 
      join trips on calendar.vsid=trips.vsid 
      join routes on routes.routeid=trips.routeid 
      join places plcstart on plcstart.placeid=trips.placeidstart 
      join places plcend on plcend.placeid=trips.placeidend 
      join segments on segments.tripid = trips.tripid 
      join stops on segments.stopid = stops.stopid 
where stops.stopid IN (:perrons0, 
:perrons1,:perrons2, 
:perrons3, :perrons4, 
:perrons5, :perrons6, 
:perrons7,:perrons8, 
:perrons9, :perrons10, 
:perrons11, :perrons12, 
:perrons13, :perrons14 
) 
AND calendar.vscdate = DATE(DATE_ADD(now(), INTERVAL "07:00" HOUR_MINUTE)) 
AND segments.segmentstart >= TIME(DATE_ADD(now(), INTERVAL "07:00" HOUR_MINUTE)) 
AND routes.routeservicetype = 0 

ORDER BY segments.segmentstart 
+0

哇這個伎倆,但我以前試過,它只是不斷加載...一定是一個服務器錯誤! – Mars

+0

@mars我很高興我可以幫助:D – reggie