Whet are things to look for when trying to optimize a mysql query that joins 7 tables?
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
There’s the query, I can’t seem to think of anything to change that’ll optimize this… it timeouts….
any tips are welcome!
What about using the
INkeyword instead of multipleOR. Also, you dont need to specifyAND segments.segmentstart > "00:00:00"since you have already provided a conditionsegments.segmentstart >= TIME(DATE_ADD(now(), INTERVAL "07:00" HOUR_MINUTE))which is greater than"00:00:00". Lastly, indexing your keys will be a good idea to optimize execution.