Mysql查看优化后的SQL 语句
EXPLAIN EXTENDED
1先执行 EXPLAIN EXTENDED
2 show warnings;
EXPLAIN EXTENDED SELECT * FROM `Test1` d WHERE d.`ap_id` IN (SELECT id FROM ap WHERE ap_code IN ( 'Test120190305192721001', 'Test120190309152839001', 'Test120190309152904001' ));
SHOW WARNINGS;
查看优化后最终执行的sql
In 并不是走的子查询 而是 join ,这个是 从上面的画红线的地方copy 出来的
SELECT `id` AS `id`, `ap_id` AS `ap_id`, `re_id` AS `re_id`, `update_time` AS `update_time`, `create_time` AS `create_time`, `deleted` AS `deleted` FROM `ap` JOIN`Test1` `d` WHERE ( ( `ap_id` =`ap`.`id` ) AND ( `ap`.`ap_code` IN ( 'Test120190305192721001', 'Test120190309152839001', 'Test120190309152904001' ) ) )
EXPLAIN EXTENDED SELECT d.* FROM `Test1` d INNER JOIN ap a ON d.`ap_id` = a.id WHERE a.ap_code IN ( 'Test120190305192721001', 'Test120190309152839001', 'Test120190309152904001' );
查看优化后的sql
SELECT `id` AS `id`, `ap_id` AS `ap_id`, `re_id` AS `re_id`, `update_time` AS `update_time`, `create_time` AS `create_time`, `deleted` AS `deleted` FROM `Test1` `d` JOIN`ap` `a` WHERE ( ( `ap_id` =`a`.`id` ) AND ( `a`.`ap_code` IN ( 'Test120190305192721001', 'Test120190309152839001', 'Test120190309152904001' ) ) );