so I have a 560mb db with the largest table 500mb(over 10 million rows)
my query hase to join 5 tables and takes about 10 seconds to finish….
SELECT DISTINCT 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 ( 43914, 23899, 23925, 23908,
23913, 19899, 23871, 43902,
23876, 25563, 18956, 19912,
23889, 23861, 23879, 23884,
23856, 19920, 19898, 23916,
23894, 20985, 23930, 20932,
20986, 22434, 20021, 19893,
19903, 19707, 19935 )
AND calendar.vscdate = Str_to_date('25-10-2011', "%e-%c-%Y")
AND segments.segmentstart >= Str_to_date('15:56', "%H:%i")
AND routes.routeservicetype = 0
AND segments.segmentstart > "00:00:00"
ORDER BY segments.segmentstart
what are things I can do to speed this up? any tips are welcome, i’m pretty new to sql…
but I can’t change the structure of the db because it’s not mine…
Just looking at the query, I would say that you should make sure that you have indexes on
trips.vsid,calendar.vscdate,segments.segmentstartandroutes.routeservicetype. I assume that there is already indexes on all the primary keys in the tables.Using
explainas Briedis suggested would show you how well the indexes work.You might want to add covering indexes for some tables, like for example an index on
trips.vsidwheretripidandrouteidare included. That way the database can use only the index for the data that is needed from the table, and not read from the actual table.Edit:
The execution plan tells you that it successfully uses indexes for everything except the
segmentstable, where it does a table scan and filters by thewherecondition. You should try to make a covering index forsegments.segmentstartby includingtripidandstopid.