slow queries
-
Trackserver 4.2.2.
The following query takes about 13 seconds in avg, up to 60 seconds.
Solution see at end of post.
Count: 18 Time=13.13s (236s) Lock=0.00s (0s) Rows=0.9 (16), wordpress_7[wordpress_7]@localhost SELECT DISTINCT t.user_id, l.trip_id AS id FROM wp_ts_tracks t INNER JOIN wp_ts_locations l ON t.id = l.trip_id INNER JOIN (SELECT t2.user_id, MAX(l2.occurred) AS endts FROM wp_ts_locations l2 INNER JOIN wp_ts_tracks t2 ON l2.trip_id = t2.id GROUP BY t2.user_id) uu ON l.occurred = uu.endts AND t.user_id = uu.user_id WHERE t.user_id IN ('3')
I never changed or added indexes, so on wp_ts_locations I have indexes on id,occurred,trip_id. And on wp_ts_tracks id and user_id.
wp_ts_tracks holds 190 entries.
wp_ts_locations holds 1313574 entries.There is only one user_id
The delay comes from this subquery:
SELECT t2.user_id, MAX(l2.occurred) AS endts FROM wp_ts_locations l2 INNER JOIN wp_ts_tracks t2 ON l2.trip_id = t2.id GROUP BY t2.user_id;First I had to do this because ‘0000-00-00 00:00:00’ was not accepted as default value:
alter table wp_ts_locations alter occurred set default ‘1980-01-01’, alter created set default ‘1980-01-01’;
Only then could I add this index which solves the problem:
create index idx_trip_occ on wp_ts_locations (trip_id,occurred);
- The topic ‘slow queries’ is closed to new replies.