MySql学习笔记(六):扫描范围

心已赠人 2022-01-17 11:37 248阅读 0赞

explain的type列表示该条查询的扫描范围,一共有七种,效果由上到下排列:

system>const>eq_ref>ref>range>index>all。

数据准备:

  1. CREATE TABLE `t_blog` (
  2. `id` int(11) NOT NULL auto_increment,
  3. `title` varchar(50) default NULL,
  4. `typeId` int(11) default NULL,
  5. PRIMARY KEY (`id`),
  6. UNIQUE KEY `title_index` (`title`),
  7. KEY `type_index` (`typeId`)
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8
  9. CREATE TABLE `t_type` (
  10. `id` int(11) NOT NULL auto_increment,
  11. `name` varchar(20) default NULL,
  12. PRIMARY KEY (`id`)
  13. ) ENGINE=InnoDB DEFAULT CHARSET=utf8

1、system

该表中只有一行记录,这种情况在日常开发中很少见,不多赘述;

2、const

表示通过索引一次就找到了结果,用于扫描主键和唯一索引,例如:

  1. mysql> explain select * from t_blog where id = 1;
  2. +----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+
  5. | 1 | SIMPLE | t_blog | const | PRIMARY | PRIMARY | 4 | const | 1 | |
  6. +----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+
  7. 1 row in set

在where子句中,id为主键且值为一个常数,在id索引中只有一条数据与之对应。

3、eq_ref

通过主键和唯一索引,只有一条数据与之匹配,例如:

  1. mysql> explain select b.* from t_blog b left join t_type t on b.typeId = t.id;
  2. +----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+
  5. | 1 | SIMPLE | b | ALL | NULL | NULL | NULL | NULL | 7 | |
  6. | 1 | SIMPLE | t | eq_ref | PRIMARY | PRIMARY | 4 | blog.b.typeId | 1 | Using index |
  7. +----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+
  8. 2 rows in set

eq_ref和const都表示在唯一索引或主键的作用下,只找到一行与之匹配的数据。const表示按主键和唯一索引读取,eq_ref通常体现在连表上,按连表的主键和唯一索引读取。

4、ref

非唯一索引扫描,有多个行与之匹配

  1. mysql> explain select * from t_blog where typeId = 4;
  2. +----+-------------+--------+------+---------------+------------+---------+-------+------+-------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+--------+------+---------------+------------+---------+-------+------+-------------+
  5. | 1 | SIMPLE | t_blog | ref | type_index | type_index | 5 | const | 1 | Using where |
  6. +----+-------------+--------+------+---------------+------------+---------+-------+------+-------------+
  7. 1 row in set

typeId是表的普通索引,即非唯一索引,与eq_ref最大的区别在于ref表示非唯一索引扫描。

5、range

表示范围,使用索引选择行,使用了 > < in beteen等

  1. mysql> EXPLAIN select * from t_blog where id>2;
  2. +----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
  5. | 1 | SIMPLE | t_blog | range | PRIMARY | PRIMARY | 4 | NULL | 3 | Using where |
  6. +----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
  7. 1 row in set

6、index

遍历索引树,读全表

  1. mysql> EXPLAIN select id from t_blog;
  2. +----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
  3. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  4. +----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
  5. | 1 | SIMPLE | t_blog | index | NULL | PRIMARY | 4 | NULL | 7 | Using index |
  6. +----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+
  7. 1 row in set

只查询id,所以只遍历索引文件即可,不需要从硬盘中读取,比all快。

7、all

读全表,不使用任何索引,从硬盘中读数据,最慢

  1. mysql> explain
  2. select * from t_blog;
  3. +----+-------------+--------+------+---------------+------+---------+------+------+-------+
  4. | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
  5. +----+-------------+--------+------+---------------+------+---------+------+------+-------+
  6. | 1 | SIMPLE | t_blog | ALL | NULL | NULL | NULL | NULL | 7 | |
  7. +----+-------------+--------+------+---------------+------+---------+------+------+-------+
  8. 1 row in set

*在一般的开发过程中,达到ref即可

转载于:https://blog.51cto.com/13593129/2361504

发表评论

表情:
评论列表 (有 0 条评论,248人围观)

还没有评论,来说两句吧...

相关阅读

    相关 OpenCV学习笔记扫描图像

    一、 图像在内存中存储方式 图像矩阵的大小取决于我们所用的颜色模型,确切地说,取决于所用通道数。如果是灰度图像,矩阵就会像这样: ![\\newcommand\{\\t

    相关 学习笔记——扫描线

    扫描线的主要步骤就是先对于一个维度进行排序扫描,并用一些数据结构维护当前扫描线所产生的贡献。(一般是用离散化+线段树) 今天就用平面上的矩阵的周长并和面积并来讲一讲扫描线。