首页 技术 正文
技术 2022年11月19日
0 收藏 842 点赞 3,096 浏览 3461 个字

本文出处:http://www.cnblogs.com/wy123/p/7366486.html 
(保留出处并非什么原创作品权利,本人拙作还远远达不到,仅仅是为了链接到原文,因为后续对可能存在的一些错误进行修正或补充,无他)

mysql执行计划中的extra列中表明了执行计划的每一步中的实现细节,其中包含了与索引相关的一些细节信息
其中跟索引有关的using index 在不同的情况下会出现Using index, Using where Using index ,Using index condition等
那么Using index 和 Using where;Using index 有什么区别?网上搜了一大把文章,说实在话也没怎么弄懂,于是就自己动手试试。

本文仅从最简单的单表去测试using index 和 using where using index以及简单测试using index condition的情况的出现时机 。
执行计划的生成与表结构,表数据量,索引结构,统计信息等等上下文等多种环境有关,无法一概而论,复杂情况另论。

测试环境搭建

  测试表以及测试数据搭建,类似于订单表和订单明细表,暂时先用订单表做测试

  测试表结构

create table test_order
(
id int auto_increment primary key,
user_id int,
order_id int,
order_status tinyint,
create_date datetime
);create table test_orderdetail
(
id int auto_increment primary key,
order_id int,
product_name varchar(100),
cnt int,
create_date datetime
);create index idx_userid_order_id_createdate on test_order(user_id,order_id,create_date);create index idx_orderid_productname on test_orderdetail(order_id,product_name);

测试数据(50W)

CREATE DEFINER=`root`@`%` PROCEDURE `test_insertdata`(IN `loopcount` INT)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
declare v_uuid varchar(50);
while loopcount>0 do
set v_uuid = uuid();
insert into test_order (user_id,order_id,order_status,create_date) values (rand()*1000,id,rand()*10,DATE_ADD(NOW(), INTERVAL - RAND()*20000 HOUR));
insert into test_orderdetail(order_id,product_name,cnt,create_date) values (rand()*100000,v_uuid,rand()*10,DATE_ADD(NOW(), INTERVAL - RAND()*20000 HOUR));
set loopcount = loopcount -1;
end while;
END

Using index VS Using where Using index

  首先,在”订单表”上,这里是一个多列复合索引
  create index idx_userid_order_id_createdate on test_order(user_id,order_id,create_date);

 

Using index 

1,查询的列被索引覆盖,并且where筛选条件是索引的是前导列,Extra中为Using index

MySQL执行计划extra中的using index 和 using where using index 的区别

Using where Using index

  1,查询的列被索引覆盖,并且where筛选条件是索引列之一但是不是索引的不是前导列,Extra中为Using where; Using index,
    意味着无法直接通过索引查找来查询到符合条件的数据

MySQL执行计划extra中的using index 和 using where using index 的区别

2,查询的列被索引覆盖,并且where筛选条件是索引列前导列的一个范围,同样意味着无法直接通过索引查找查询到符合条件的数据

MySQL执行计划extra中的using index 和 using where using index 的区别

   

NULL(既没有Using index,也没有Using where Using index,也没有using where

  1,查询的列未被索引覆盖,并且where筛选条件是索引的前导列,
       意味着用到了索引,但是部分字段未被索引覆盖,必须通过“回表”来实现,不是纯粹地用到了索引,也不是完全没用到索引,Extra中为NULL(没有信息)

MySQL执行计划extra中的using index 和 using where using index 的区别

Using where

  1,查询的列未被索引覆盖,where筛选条件非索引的前导列,Extra中为Using where

MySQL执行计划extra中的using index 和 using where using index 的区别

  2,查询的列未被索引覆盖,where筛选条件非索引列,Extra中为Using where

  MySQL执行计划extra中的using index 和 using where using index 的区别

  using where 意味着通过索引或者表扫描的方式进程where条件的过滤,
  反过来说,也就是没有可用的索引查找,当然这里也要考虑索引扫描+回表与表扫描的代价。
  这里的type都是all,说明MySQL认为全表扫描是一种比较低的代价。

 Using index condition

  1,查询的列不全在索引中,where条件中是一个前导列的范围

  MySQL执行计划extra中的using index 和 using where using index 的区别

  2,查询列不完全被索引覆盖,查询条件完全可以使用到索引(进行索引查找)

  MySQL执行计划extra中的using index 和 using where using index 的区别

  参考:MySQL · 特性分析 · Index Condition Pushdown (ICP)
  using index conditoin 意味着查询列的某一部分无法直接使用索引
  上述case1中,
  如果禁用ICP(set optimizer_switch=’index_condition_pushdown=off’),
  执行计划是using where,意味着全表扫描,如果启用ICP,执行计划为using index Condition,意味着在筛选的过程中实现过滤
  上述case1中
  第二个查询条件无法直接使用索引,隐含了一个查找+筛选的过程。
  两个case的共同点就是无法直接使用索引。

结论:
  1,Extra中的为Using index的情况
    where筛选列是索引的前导列 &&查询列被索引覆盖 && where筛选条件是一个基于索引前导列的查询,意味着通过索引超找就能直接找到符合条件的数据,并且无须回表

  2,Extra中的为空的情况
    查询列存在未被索引覆盖&&where筛选列是索引的前导列,意味着通过索引超找并且通过“回表”来找到未被索引覆盖的字段,

3,Extra中的为Using where Using index:
  出现Using where Using index意味着是通过索引扫描(或者表扫描)来实现sql语句执行的,即便是索引前导列的索引范围查找也有一点范围扫描的动作,不管是前非索引前导列引起的,还是非索引列查询引起的。

20200313补充,这里有一个很经典的例子,会出现using index condition;using where的情况,这种情况暂时不知道怎么去解释,按道理也不是ICP??
https://juejin.im/post/5cef832e5188257c6b516d1f
using index condition;using where出现的时候,实际上是分两步
1,第一步是ICP,在存储引擎层发生的
2,第二步是using where,是在server层过滤的。
MySQL执行计划extra中的using index 和 using where using index 的区别

尚未解决的问题:

     查询1

MySQL执行计划extra中的using index 和 using where using index 的区别

查询2

MySQL执行计划extra中的using index 和 using where using index 的区别

    

    查询3(逻辑上等价于查询1+查询2),执行计划发生了很大的变化。

MySQL执行计划extra中的using index 和 using where using index 的区别

总结:

  MySQL执行计划中的Extra中信息非常多,不仅仅包括Using index,Using where Using index,Using index condition,Using where,尤其是在多表连接的时候,这一点在相对MSSQL来说,不够直观或者结构化。
  MSSQL中是通过区分索引查找(index seek),索引扫描(index scan),表扫描(table scan)来实现具体的查询的,这图形化的执行计划在不同的场景下是非常直观的,要想完全弄懂MySQL的这个执行计划,可能要更多地在实践中摸索。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290