MySql学习笔记(六):扫描范围
发表于:2024-10-21 作者:千家信息网编辑
千家信息网最后更新 2024年10月21日,explain的type列表示该条查询的扫描范围,一共有七种,效果由上到下排列:system>const>eq_ref>ref>range>index>all。数据准备:CREATE TABLE `t
千家信息网最后更新 2024年10月21日MySql学习笔记(六):扫描范围
explain的type列表示该条查询的扫描范围,一共有七种,效果由上到下排列:
system>const>eq_ref>ref>range>index>all。
数据准备:
CREATE TABLE `t_blog` ( `id` int(11) NOT NULL auto_increment, `title` varchar(50) default NULL, `typeId` int(11) default NULL, PRIMARY KEY (`id`), UNIQUE KEY `title_index` (`title`), KEY `type_index` (`typeId`)) ENGINE=InnoDB DEFAULT CHARSET=utf8CREATE TABLE `t_type` ( `id` int(11) NOT NULL auto_increment, `name` varchar(20) default NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8
1、system
该表中只有一行记录,这种情况在日常开发中很少见,不多赘述;
2、const
表示通过索引一次就找到了结果,用于扫描主键和唯一索引,例如:
mysql> explain select * from t_blog where id = 1;+----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+| 1 | SIMPLE | t_blog | const | PRIMARY | PRIMARY | 4 | const | 1 | |+----+-------------+--------+-------+---------------+---------+---------+-------+------+-------+1 row in set
在where子句中,id为主键且值为一个常数,在id索引中只有一条数据与之对应。
3、eq_ref
通过主键和唯一索引,只有一条数据与之匹配,例如:
mysql> explain select b.* from t_blog b left join t_type t on b.typeId = t.id;+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+| 1 | SIMPLE | b | ALL | NULL | NULL | NULL | NULL | 7 | || 1 | SIMPLE | t | eq_ref | PRIMARY | PRIMARY | 4 | blog.b.typeId | 1 | Using index |+----+-------------+-------+--------+---------------+---------+---------+---------------+------+-------------+2 rows in set
eq_ref和const都表示在唯一索引或主键的作用下,只找到一行与之匹配的数据。const表示按主键和唯一索引读取,eq_ref通常体现在连表上,按连表的主键和唯一索引读取。
4、ref
非唯一索引扫描,有多个行与之匹配
mysql> explain select * from t_blog where typeId = 4;+----+-------------+--------+------+---------------+------------+---------+-------+------+-------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+--------+------+---------------+------------+---------+-------+------+-------------+| 1 | SIMPLE | t_blog | ref | type_index | type_index | 5 | const | 1 | Using where |+----+-------------+--------+------+---------------+------------+---------+-------+------+-------------+1 row in set
typeId是表的普通索引,即非唯一索引,与eq_ref最大的区别在于ref表示非唯一索引扫描。
5、range
表示范围,使用索引选择行,使用了 > < in beteen等
mysql> EXPLAIN select * from t_blog where id>2;+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+| 1 | SIMPLE | t_blog | range | PRIMARY | PRIMARY | 4 | NULL | 3 | Using where |+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+1 row in set
6、index
遍历索引树,读全表
mysql> EXPLAIN select id from t_blog;+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+| 1 | SIMPLE | t_blog | index | NULL | PRIMARY | 4 | NULL | 7 | Using index |+----+-------------+--------+-------+---------------+---------+---------+------+------+-------------+1 row in set
只查询id,所以只遍历索引文件即可,不需要从硬盘中读取,比all快。
7、all
读全表,不使用任何索引,从硬盘中读数据,最慢
mysql> explain select * from t_blog;+----+-------------+--------+------+---------------+------+---------+------+------+-------+| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |+----+-------------+--------+------+---------------+------+---------+------+------+-------+| 1 | SIMPLE | t_blog | ALL | NULL | NULL | NULL | NULL | 7 | |+----+-------------+--------+------+---------------+------+---------+------+------+-------+1 row in set
*在一般的开发过程中,达到ref即可
索引
数据
只有
非唯
范围
一行
硬盘
开发
查询
普通
最大
作用
多个
子句
常数
情况
效果
文件
结果
过程
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
姜堰区工业网络技术联系方式
服务器怎样重装系统
龙川定制软件开发
网络技术专业用什么电脑好
国家网络安全宣传周证书有用吗
数据库结构图的工具
分布式数据库性能检测
香港巴士数据库
什么样的数据库不能备份
嘉盛mt4服务器
标签打印机数据库无法断开
中国移动dns服务器地址山东
北京麦克帮网络技术
六安手机软件开发定制公司
软件开发螺旋迭代
惠普服务器电源1200瓦
修改系统时间是否重启数据库服务
单台内网服务器获取北京时间
流氓软件开发商
查看数据库中的用户
手抄报网络安全文章
服务器域名是什么意思
江苏gps网络时间服务器价格
网络安全法宣传的意义
恩智浦软件开发
网络安全知识手册大全
中班幼儿网络安全教育PPt
专业上网管理服务器
游戏服务器账号管理
睢宁度汛德网络技术服务中心