MySQL查询不使用索引

时间:2014-09-25 21:55:47

标签: mysql indexing full-table-scan

以下查询:

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'

使用Explain后,很明显查询使用表扫描: enter image description here

有各种密钥,pusher_idx包含device_idflight_id。为什么不使用这个索引?

1 个答案:

答案 0 :(得分:0)

在这种情况下,mysql优化器改变了表的顺序(它可以做到 - 它们都是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'

因此,只要flights_followed没有被任何自己的谓词过滤(使用新的加入顺序,其他表加入了它,所以它们被过滤了) - 它决定执行完整扫描。