`

MYSQL的全表扫描,主键索引(聚集索引、第一索引),非主键索引(非聚集索引、第二索引),覆盖索引四种不同查询的分析

 
阅读更多

 

MYSQL的全表扫描,主键索引(聚集索引、第一索引),非主键索引(非聚集索引、第二索引),覆盖索引四种不同查询的分析

 

1.前置条件:

本次是基于小数据量,且数据块在一个页中的最理想情况进行分析,可能无具体的实际意义,但是可以借鉴到各种复杂条件下,因为原理是相同的,知小见大,见微知著!

 

打开语句分析并确认是否已经打开

 

mysql> set profiling=1;   
Query OK, 0 rows affected (0.00 sec)

mysql> select @@profiling;
+-------------+
| @@profiling |
+-------------+
|           1 |
+-------------+
1 row in set (0.01 sec)
 

2.数据准备:

2.1全表扫描数据

 

create table person4all(id int not null  auto_increment, name varchar(30) not null, gender varchar(10) not null ,primary key(id));
insert into person4all(name,gender) values("zhaoming","male");
insert into person4all(name,gender) values("wenwen","female");
 

2.2根据主键查看数据

 

create table person4pri(id int not null  auto_increment, name varchar(30) not null, gender varchar(10) not null ,primary key(id));
insert into person4pri(name,gender) values("zhaoming","male");
insert into person4pri(name,gender) values("wenwen","female");
 

2.3根据非聚集索引查数据

 

create table person4index(id int not null  auto_increment, name varchar(30) not null, gender varchar(10) not null ,primary key(id) , index(gender));
insert into person4index(name,gender) values("zhaoming","male");
insert into person4index(name,gender) values("wenwen","female");
 

2.4根据覆盖索引查数据

 

create table person4cindex(id int not null  auto_increment, name varchar(30) not null, gender varchar(10) not null ,primary key(id) , index(name,gender));
insert into person4cindex(name,gender) values("zhaoming","male");
insert into person4cindex(name,gender) values("wenwen","female");
 

主要从以下几个方面分析:查询消耗的时间,走的执行计划等方面。

 

3.开工测试:

第一步:全表扫描

 

mysql> select * from person4all ;
+----+----------+--------+
| id | name     | gender |
+----+----------+--------+
|  1 | zhaoming | male   |
|  2 | wenwen   | female |
+----+----------+--------+
2 rows in set (0.00 sec)
 

查看其执行计划:

 

mysql> explain select * from person4all;
+----+-------------+------------+------+---------------+------+---------+------+------+-------+
| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra |
+----+-------------+------------+------+---------------+------+---------+------+------+-------+
|  1 | SIMPLE      | person4all | ALL  | NULL          | NULL | NULL    | NULL |    2 |       |
+----+-------------+------------+------+---------------+------+---------+------+------+-------+
1 row in set (0.01 sec)
 

我们可以很清晰的看到走的是全表扫描,而没有走索引!

 

查询消耗的时间:

 

mysql> show profiles;
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------+
| Query_ID | Duration   | Query                                                                                                                             |
|       54 | 0.00177300 | select * from person4all                                                                                                          |
|       55 | 0.00069200 | explain select * from person4all                                                                                                  |
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------+
 

全表扫描总共话了0.0017730秒

 

各个阶段消耗的时间是:

 

mysql> show profile for query 54;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000065 |
| checking query cache for query | 0.000073 |
| Opening tables                 | 0.000037 |
| System lock                    | 0.000024 |
| Table lock                     | 0.000053 |
| init                           | 0.000044 |
| optimizing                     | 0.000022 |
| statistics                     | 0.000032 |
| preparing                      | 0.000030 |
| executing                      | 0.000020 |
| Sending data                   | 0.001074 |
| end                            | 0.000091 |
| query end                      | 0.000020 |
| freeing items                  | 0.000103 |
| storing result in query cache  | 0.000046 |
| logging slow query             | 0.000019 |
| cleaning up                    | 0.000020 |
+--------------------------------+----------+
17 rows in set (0.00 sec)
 

第一次不走缓存的话,需要检查是否存在缓存中,打开表,初始化等操作,最大的开销在于返回数据。

 

第二步:根据主键查询数据。

 

mysql> select name ,gender from person4pri where id in (1,2);
+----------+--------+
| name     | gender |
+----------+--------+
| zhaoming | male   |
| wenwen   | female |
+----------+--------+
2 rows in set (0.01 sec)
 

查看其执行计划:

 

mysql> explain select name ,gender from person4pri where id in (1,2);
+----+-------------+------------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table      | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+------------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | person4pri | range | PRIMARY       | PRIMARY | 4       | NULL |    2 | Using where |
+----+-------------+------------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)
 

从执行计划中我们可以看出,走的是范围索引。

 

再看其执行消耗的时间:

 

mysql> show profiles;
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------+
| Query_ID | Duration   | Query                                                                                                                             |
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------+
|       63 | 0.00135700 | select name ,gender from person4pri where id in (1,2)                                                                             |
|       64 | 0.00079200 | explain select name ,gender from person4pri where id in (1,2)                                                                     |
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------+
15 rows in set (0.01 sec)
 

这次查询消耗时间为0.00079200。

 

查看各个阶段消耗的时间:

 

mysql> show profile for query 63;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000067 |
| checking query cache for query | 0.000146 |
| Opening tables                 | 0.000342 |
| System lock                    | 0.000027 |
| Table lock                     | 0.000115 |
| init                           | 0.000056 |
| optimizing                     | 0.000032 |
| statistics                     | 0.000069 |
| preparing                      | 0.000039 |
| executing                      | 0.000022 |
| Sending data                   | 0.000100 |
| end                            | 0.000075 |
| query end                      | 0.000022 |
| freeing items                  | 0.000158 |
| storing result in query cache  | 0.000045 |
| logging slow query             | 0.000019 |
| cleaning up                    | 0.000023 |
+--------------------------------+----------+
17 rows in set (0.00 sec)
 

看出最大的消耗也是在Sending data,第一次也是需要一些初始化操作。

 

第三步:根据非聚集索引查询

 

mysql> select name ,gender from person4index where gender in ("male","female");
+----------+--------+
| name     | gender |
+----------+--------+
| wenwen   | female |
| zhaoming | male   |
+----------+--------+
2 rows in set (0.00 sec)

查看器执行计划:

 

mysql> explain select name ,gender from person4index where gender in ("male","female");
+----+-------------+--------------+-------+---------------+--------+---------+------+------+-------------+
| id | select_type | table        | type  | possible_keys | key    | key_len | ref  | rows | Extra       |
+----+-------------+--------------+-------+---------------+--------+---------+------+------+-------------+
|  1 | SIMPLE      | person4index | range | gender        | gender | 12      | NULL |    2 | Using where |
+----+-------------+--------------+-------+---------------+--------+---------+------+------+-------------+
1 row in set (0.00 sec)
 

可以看出,走的也是范围索引。同主键查询,那么就看其消耗时间了

 

mysql> show profiles;
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
| Query_ID | Duration   | Query                                                                                                                                               |
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
|       68 | 0.00106600 | select name ,gender from person4index where gender in ("male","female")                                                                             |
|       69 | 0.00092500 | explain select name ,gender from person4index where gender in ("male","female")                                                                     |
+----------+------------+-----------------------------------------------------------------------------------------------------------------------------------------------------+
15 rows in set (0.00 sec)
 

这个非主键索引消耗的时间为:0.00106600,可以看出略大于组件索引消耗的时间。

 

看其具体消耗的阶段:

 

mysql> show profile for query 68 ;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000059 |
| checking query cache for query | 0.000111 |
| Opening tables                 | 0.000085 |
| System lock                    | 0.000023 |
| Table lock                     | 0.000067 |
| init                           | 0.000183 |
| optimizing                     | 0.000031 |
| statistics                     | 0.000139 |
| preparing                      | 0.000035 |
| executing                      | 0.000020 |
| Sending data                   | 0.000148 |
| end                            | 0.000024 |
| query end                      | 0.000019 |
| freeing items                  | 0.000043 |
| storing result in query cache  | 0.000042 |
| logging slow query             | 0.000017 |
| cleaning up                    | 0.000020 |
+--------------------------------+----------+
17 rows in set (0.00 sec)
 

看几个关键词的点;init,statistics,Sending data 这几个关键点上的消耗向比较主键的查询要大很多,特别是Sending data。因为若是走的非聚集索引,那么就需要回表进行再进行一次查询,多消耗一次IO。

 

第四部:根据覆盖索引查询数据

 

mysql> select gender ,name from person4cindex where gender in ("male","female");
+--------+----------+
| gender | name     |
+--------+----------+
| female | wenwen   |
| male   | zhaoming |
+--------+----------+
2 rows in set (0.01 sec)
 

这里需要注意的是,我的字段查询顺序变了,是gender,name而不在是前面的name,gender,这样是为了走覆盖索引。具体看效果吧

 

还是先看执行计划:

 

mysql> explain select gender ,name from person4cindex where gender in ("male","female");
+----+-------------+---------------+-------+---------------+------+---------+------+------+--------------------------+
| id | select_type | table         | type  | possible_keys | key  | key_len | ref  | rows | Extra                    |
+----+-------------+---------------+-------+---------------+------+---------+------+------+--------------------------+
|  1 | SIMPLE      | person4cindex | index | NULL          | name | 44      | NULL |    2 | Using where; Using index |
+----+-------------+---------------+-------+---------------+------+---------+------+------+--------------------------+
1 row in set (0.00 sec)
 

最后栏Extra中表示走的就是覆盖索引。

 

看消耗的时间吧:

 

mysql> show profiles;
+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Query_ID | Duration   | Query                                                                                                                                                            |
+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
|       83 | 0.00115400 | select gender ,name from person4cindex where gender in ("male","female")                                                                                         |
|       84 | 0.00074000 | explain select gender ,name from person4cindex where gender in ("male","female")                                                                                 |
+----------+------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+
 

我们看到消耗的时间是0.00115400,看这个数字好像挺高的,那么都花在什么地方了呢?

 

看下具体的消耗情况:

 

mysql> show profile for query 83 ;
+--------------------------------+----------+
| Status                         | Duration |
+--------------------------------+----------+
| starting                       | 0.000083 |
| checking query cache for query | 0.000113 |
| Opening tables                 | 0.000039 |
| System lock                    | 0.000026 |
| Table lock                     | 0.000075 |
| init                           | 0.000128 |
| optimizing                     | 0.000193 |
| statistics                     | 0.000056 |
| preparing                      | 0.000038 |
| executing                      | 0.000021 |
| Sending data                   | 0.000121 |
| end                            | 0.000042 |
| query end                      | 0.000021 |
| freeing items                  | 0.000112 |
| storing result in query cache  | 0.000043 |
| logging slow query             | 0.000021 |
| cleaning up                    | 0.000022 |
+--------------------------------+----------+
17 rows in set (0.00 sec)
 

很惊奇吧,在初始化和优化上消耗了这么多时间,取数据基恩差不多。

 

总结:

    有了上面这些数据,那么我们整理下吧。未存在缓存下的数据。

 

看这个表,全表扫描最慢,我们可以理解,同时主键查询比覆盖所有扫描慢也还能接受,但是为什么主键扫描会比非主键扫描慢?而且非主键查询需要消耗的1次查询的io+一次回表的查询IO,理论上是要比主键扫描慢,而出来的数据缺不是如此。那么就仔细看下是个查询方式在各个主要阶段消耗的时间吧。

查询是否存在缓存,打开表及锁表这些操作时间是差不多,我们不会计入。具体还是看init,optimizing等环节消耗的时间。

 

 

1.从这个表中,我们看到非主键索引和覆盖索引在准备时间上需要开销很多的时间,预估这两种查询方式都需要进行回表操作,所以花在准备上更多时间。

2.第二项optimizing上,可以清晰知道,覆盖索引话在优化上大量的时间,这样在二级索引上就无需回表。

3. Sendingdata,全表扫描慢就慢在这一项上,因为是加载所有的数据页,所以花费在这块上时间较大,其他三者都差不多。

4. 非主键查询话在freeingitems上时间最少,那么可以看出它在读取数据块的时候最少。

5.相比较主键查询和非主键查询,非主键查询在Initstatistics都远高于主键查询,只是在freeingitems开销时间比主键查询少。因为这里测试数据比较少,但是我们可以预见在大数据量的查询上,不走缓存的话,那么主键查询的速度是要快于非主键查询的,本次数据不过是太小体现不出差距而已。

6.在大多数情况下,全表扫描还是要慢于索引扫描的。

 

 

tips:

过程中的辅助命令:

1.清楚缓存

reset query cache ;

flush tables;

 

2.查看表的索引:

show index from tablename;

2
0
分享到:
评论
3 楼 syw19901001 2014-12-06  
30多条mysql数据库优化方法,千万级数据库记录查询轻松解决
http://www.ihref.com/read-16422.html
2 楼 inter12 2012-02-29  
lmxbitihero 写道
mysql做同样查询执行时间不一定一样。得根据cpu,内存,硬盘的情况而定。你的思路很好,但是最好是弄个大数据量,否则没说服力。

嗯,对于你这点我在文章开始就已经提到了,只是给大家提供一个解决思路的思路。因为不同的环境下影响因素很多,所以若是我在这提出一个我自己的复杂环境下的数据也并不能带给大家多少帮助,还不如在一个单纯的环境下展示怎么去处理这类问题。
1 楼 lmxbitihero 2012-02-28  
mysql做同样查询执行时间不一定一样。得根据cpu,内存,硬盘的情况而定。你的思路很好,但是最好是弄个大数据量,否则没说服力。

相关推荐

    索引介绍聚集索引和非聚集索引

    关于索引的介绍,以及b+树结构图,两种索引性能比较,索引优化建议

    MySQL索引之主键索引

    上次的分享我们介绍了聚集索引和非聚集索引的区别,本次我们继续介绍主键索引和辅助索引的区别。 1、主键索引 主键索引,简称主键,原文是PRIMARY KEY,由一个或多个列组成,用于唯一性标识数据表中的某一条记录。一...

    MySQL 主键与索引的联系与区别分析

    所谓主键就是能够唯一标识表中某一行的属性或属性组,一个表只能有一个主键,但可以有多个候选索引。因为主键可以唯一标识某一行记录,所以可以确保执行数据更新、删除的时候不会出现张冠李戴的错误。主键除了上述...

    MySQL索引 聚集索引

    MySQL索引 聚集索引 如果你想了解MySQL索引查询优化,你首先应该对MySQL数据组织结构、B-Tree索引、聚集索引,次要索引有一定的了解,才能够更好地理解MySQL查询优化行为。这里主要探讨MySQL InnoDB的聚集索引。

    JAVA面试题MySQL索引原理及索引优化校招面试找工作笔试

    第一点、第二点、第三点 不建议使用过长的字段,不建议使用非自增字段作为主键 什么条件下使用索引? 索引选择性,行数前缀索引 所谓索引的选择性是指不重复的索引值与表记录数的比值 索引优化 最左前缀原则 Hash...

    MySQL数据库:创建索引.pptx

    使用CREATE INDEX语句可以在一个已有表上创建索引,一个表可以创建多个索引。 语法格式: CREATE [UNIQUE | FULLTEXT] INDEX 索引名 ON 表名(列名[(长度)] [ASC | DESC],...) 说明: UNIQUE:表示创建的是唯一性索引 ...

    关于MySQL面试题中有关索引的九大难点全在这里了

    o非聚集索引:非聚集索引就是以非主键创建的索引,在叶子节点存储的是主键和索引列。 逻辑维度 o主键索引:一种特殊的唯一索引,不允许有空值。 o普通索引:MySQL中基本索引类型,允许空值和重复值。 o联合索引:多...

    Mysql-索引原理分析

    1 .索引的存储结构是什么? 是B树、B+树还是二叉树 2.什么是聚集索引? 聚集索引的主键索引和次要索引区别是什么 3.什么是非聚集索引? 非聚集索引的主键索引和次要索引区别是什么

    主键索引与唯一索引的区别

    1、 主键是一种约束,唯一索引是一种索引,两者在本质上是不同的。 2、 主键创建后一定包含一个唯一性索引,唯一性索引并不一定就是主键。 3、 唯一性索引列允许空值,而主键列不允许为空值。 4、 主键列在创建时...

    MySql练习4:创建学生表和成绩表索引并查看索引.zip

    MySql练习4:创建学生表和成绩表索引并查看索引.zip MySql练习4:创建学生表和成绩表索引并查看索引.zip MySql练习4:创建学生表和成绩表索引并查看索引.zip

    MySQL数据库:索引概述.pptx

    索引用来快速地寻找那些具有特定值的记录,如果没有索引,执行查询时MySQL必须从第一个记录开始扫描整个表的所有记录,直至找到符合要求的记录。表里面的记录数量越多,这个操作的代价就越高。 索引提供指针以指向...

    mysql中创建各种索引的语句整理.pdf

    Mysql中创建各种索引的语句整理 ...mysql>ALTER TABLE `table_name` ADD INDEX index_name (`column1`, `column2`, 、where条件列 、排序列或者分组列 、主键本身就是索引,无需再次添加

    MySQL Innodb 索引原理详解

    MySQL Innodb 索引原理详解

    mysql多条件索引

    数据库的索引可以加快查询速度,原因是索引使用特定的数据结构(B-Tree)对特定的列额外组织存放,加快存储引擎(索引是存储引擎实现)查找记录...所以有些时候(如按顺序读取数据)全表扫描会比使用索引快的原因就在于此。

    mysql索引失效.docx MySQL索引失效是指在查询执行过程中,数据库无法有效地使用索引来提高查询性能

    使用了不适当的数据类型:如果列的数据类型与索引的数据类型不匹配,MySQL可能...查询范围过大:如果查询的结果集涵盖了太多的行,MySQL可能会放弃使用索引而选择全表扫描。这是因为在这种情况下,全表扫描可能更快。

    MySQL中主键索引与聚焦索引之概念的学习教程

    主键索引,简称主键,原文是PRIMARY KEY,由一个或多个列组成,用于唯一性标识数据表中的某一条记录。一个表可以没有主键,但最多只能有一个主键,并且主键值不能包含NULL。 在MySQL中,InnoDB数据表的主键设计我们...

    MySql索引详解,索引可以大大提高MySql的检索速度

    创建索引时,你需要确保该索引是应用在SQL查询语的条件(一般作为WHERE 子句的条件)实际上,索引也是一张表,该表保存了主键与索引字段,并指向实体表的记录。上面都在说使用索引的好处,但过多的使用索引将会造成...

    Mysql如何避免全表扫描的方法

    在以下几种条件下,MySQL就会做全表扫描: 1>数据表是在太小了,做一次全表扫描比做索引键的查找来得快多了。当表的记录总数小于10且记录长度比较短时通常这么做。 2>没有合适用于 ON 或 WHERE 分句的索引字段。 3>...

    mysql存储与索引技术

    介绍mysql不同的存储引擎 以及 索引技术在mysql中的应用

Global site tag (gtag.js) - Google Analytics