简体   繁体   English

MySQL查询不使用索引

[英]MySQL query does not use index

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: 使用Explain之后,很明显查询使用表扫描: 在此处输入图片说明

There are various keys and pusher_idx contains both device_id and flight_id . 有各种键, pusher_idx包含device_idflight_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 ): 在这种情况下,mysql Optimizer更改了表的顺序(可以做到-它们都是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. 因此,一旦flights_followed没有被任何自己的谓词过滤(使用新的连接顺序,其他表flights_followed连接到它,因此被过滤的是它们)–它决定执行完整扫描。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM