-- access_type 表查询联接类型,由上到下,性能逐步变低
-- 1. NULL 不访问任何一个表
EXPLAIN select 1 from dual;
-- 2. system 根据主键查询系统表且这个表只有一条记录【特殊的const场景】
-- 3. const 常量查询非常快。主键或者唯一索引的常量查询,表格最多只有1行记录符合查询。
EXPLAIN select * from myshop.ecs_users where user_id =1;
-- 4. eq_ref 使用PRIMARYKEY或者UNIQUE 和前面的结果集匹配。
EXPLAIN select * from myshop.ecs_order_info b, myshop.ecs_users a where b.user_id = a.user_id;
-- 5. ref 非聚集索引的常量查询。
EXPLAIN select * from myshop.ecs_users where email = 'onlyoneemail.com';
-- 6. fulltext 查询的过程中,使用到了 fulltext 索引(fulltext index在innodb引擎中,只有5.6版本之后的支持)
EXPLAIN SELECT * FROM `demo-fulltext` WHERE MATCH(`remark`) AGAINST('Tony');
-- 7. ref_or_null 跟ref查询类似,在ref的查询基础上,加多一个null值的条件查询
EXPLAIN select * from myshop.ecs_users where email = 'onlyoneemail.com' OR email is null;
-- 8. index_merge 索引合并(分别两个查询条件的结果,再合并)
EXPLAIN select * from myshop.ecs_users where email = 'onlyoneemail.com' OR user_id = 1;
-- 9. unique_subquery IN子查询的结果由聚族索引或唯一索引覆盖。
SET optimizer_switch='materialization=off';
EXPLAIN select * from myshop.ecs_users where user_id not in (
select user_id from myshop.ecs_users where email like '%.com%' );
SET optimizer_switch='materialization=on';
-- 10. index_subquery 与unique_subquery类似,但是用的是二级索引
SET optimizer_switch='materialization=off';
EXPLAIN select * from myshop.ecs_users where email not in (
select email from myshop.ecs_users where email like '%.com%' );
SET optimizer_switch='materialization=on';
-- 11. range =、<>、>、>=、<、<=、IS NULL、BETWEEN、IN、<=> (这是个表达式:左边可以推出右边,右边也可推出左边)
EXPLAIN select order_id from myshop.ecs_order_info where order_id < 10;
-- 12. index 执行full index scan直接从索引中取的想要的结果数据,也就是可以避免回表
EXPLAIN select order_id from myshop.ecs_order_info;
-- 13. ALL 执行full table scan,这事最差的一种方式
EXPLAIN select pay_fee from myshop.ecs_order_info;

如果觉得有收获,欢迎点赞和评论,更多知识,请点击关注查看我的主页信息哦~
网友评论