The following query:
EXPLAIN
SELECT
fdb . * ,
TIME_FORMAT( fdb.scheme, '%H:%i' ) AS scheme,
TIME_FORMAT( fdb.actual, '%H:%i' ) AS actual,
TIME_FORMAT( fdb.baggage, '%H:%i' ) AS baggage,
TIME_FORMAT( fdb.baggage_handled, '%H:%i' ) AS baggage_handled,
ff . * , TIME_FORMAT( ff.actual_saved, '%H:%i' ) AS actual_saved,
TIME_FORMAT( ff.baggage_saved, '%H:%i' ) AS baggage_saved,
TIME_FORMAT( ff.baggage_handled_saved, '%H:%i' ) AS baggage_handled_saved,
ap.device_id,
ap.device_token,
ap.device_language,
ap.app_edition,
ap.receive_status_notifications,
ap.receive_time_notifications,
ap.receive_luggage_notifications,
ap.receive_gate_notifications,
ap.receive_runway_notifications,
ap.receive_plane_notifications
FROM flights_database fdb
JOIN flights_followed ff ON fdb.flight_id = ff.flight_id
JOIN apns_users ap ON ff.device_id = ap.device_id
AND ap.app_edition = '1'
After using Explain, it is clear the query uses table scans:
There are various keys and pusher_idx
contains both device_id
and flight_id
. Why is this index not used?
In this case mysql optimizer changed the order of your tables (it can do it - they are all INNER JOINS
):
SELECT
...
FROM flights_followed ff
JOIN flights_database fdb ON fdb.flight_id = ff.flight_id
JOIN apns_users ap ON ff.device_id = ap.device_id
AND ap.app_edition = '1'
So as soon as flights_followed
is not filtered by any own predicate (with new join order it's other tables joined to it, so it's they that are filtered) - it decides to perform a full scan.