当前位置:网站首页>Explain detailed explanation and practice
Explain detailed explanation and practice
2022-08-04 04:06:00 【Laughing_Xie】
Explain是mysql提供的工具,用于分析SQL语句的执行情况
图1.

图2.
show warnings; 查看MySQL内部sql优化的情况

Turn off derived table merge optimization,Show more detailed steps
set session optimizer_switch='derived_merge=off';

Three queries are involved,DERIVED (衍生查询,从user中查), SUBQUERY(子查询,table为 client),This is a complex query PRIMARY(最外层的,对应的table为 衍生表<derived3> ,derived ,id 为3 )

图3.

select_type:
1.在执行SQL语句过程中,idThe larger the value, the higher the priority of execution(也有相同的情况)
DERIVED :Usually executed first,包含在from The query result set generated after that,生成一个临时表.MySQL会将结果存放在一个临时表中,也称为派生表
SIMPLE :The query statement is a simple query,如图1,没有关联,Subqueries, etc
PRIMARY :查询包含子查询的时候,Corresponds to the outermost layer in a complex queryselect 语句
UNION : union之后出现的select语句对应的查询类型、
SUBQUERY :子查询
DEPENDENTSUBQUERY : 取决于外面的查询 目前第一步从user表中查询;
2.然后执行的类型为PRIMARY包含有子查询,从派生表里面查询数据 ;
3.目前表是没有分区的 所以partitions为空
4.type 代表的是当前通过什么样的方式对数据库表进行访问,Association type or access type(MySQL决定如何查找表中的行,查找数据行记录的大概范围)
一般常用 system >const >eq_ref >ref >range>index>ALL,Generally, it is necessary to ensure that the query is reachedrange级别,最好达到ref级别,Efficiency gradually diminishes.
NULL: MySQL能够在优化阶段分解查询语句,在执行阶段用不着再访问表或索引.例如查找min(id),底层B+ThreeIt was already sorted,very easy to get.The lookup index alone is done,The table is accessed when execution is not required.

system : 该表只有一行,相当于系统表,查询速度很快,读取1次,system是const类型的特例,There is only one result set in itself,Just like checking constants,directly checked out.
const : 说明在进行数据查询的时候,命中了primary key或唯一索引,此类数据查询非常快
eq_ref : primary key或者unique key 索引的所有部分被连接使用,最多只会返回一条符合条件的记录.这可能是在const之外最好的连接类型了

ref : If the hit index is a secondary index,不是unique key,type是ref,如果是多字段的联合索引,根据最左匹配原则,从联合索引的最左侧开始连续多个列的字段进行等值比较也是ref类型.
ref_or_null : 类似于ref ,区别在于会额外搜索包含NULL值的行
unique_subquery : 在where条件中的关于in的子查询集合
index_subquery : 用于非唯一索引,可以返回重复值
range : Indexes are used for row data retrieval,只对指定范围内的行数据检索.就是针对的一个有索引的字段,在指定的范围中检索数据 如果where 语句中使用 between、and 、< 、> 、<=、 in 等条件都是range

index : 与ALL都是读全表,区别在于index是走二级索引(全索引扫描),而ALL是从硬盘中读取.【If the result set of the lookup,That is, in the secondary index there,In the primary key index also has,Sometimes it is optimized to choose a secondary index,Because the primary key index as a whole is larger than the secondary index,Secondary indexes are scanned first,The amount of disk occupied is also small,Because it is smaller than the primary key index;具体如何选,内部有自己的算法】

The following is a full table scan,如果是index,It is a full index scan
ALL : The primary key index to scan,It doesn't start from the root node,而是从最小的值开始,Do a traversal full table scan.进行数据匹配,此时的数据查询性能最差(The ratio of the amount of disk data readindex更大一点).
5.possible_keys (和keyPut the index of this table)
表示哪些索引可以按MySQL的优化器进行选择,也就是索引候选者有哪些.
6. key
7. key_len
sql语句执行过程中,The length of the index used specifically(比如联合索引,只用了其中一个,通过key_lenIt is also possible to distinguish which index is used,If all are used, it is the total length),和实际的字段属性以及是否为null都有关系,There are different ways of calculating the length,一般tinyint:1字节,smallint:2字节 ,int:4字节 bigint:8字节 ,date: 3字节 ,timestamp:4字节,datetime:8字节 ,If the field runs empty,One more byte is needed to record whether it isNULL
8.ref (条件的引用方式)
如果使用字段进行常量等值查询时ref 此处为const ;当查询条件中使用了表达式或者函数ref 为 func ;其他的显示 null;
9.rows
执行查询的时候必须检查的行数,行数越少,效率越高!!
10. filtered
是一个百分比的值,rows*fitered/100 可以估算出将要和explain 中前一个表进行连接的行数(前一个表指explain中的id值比当前表id值小的表)
11.extra
其他列不显示的额外信息在此列进行展示.
Using index :数据库使用了覆盖索引,查询的列被索引覆盖,使用到覆盖索引会比较快
Using where : 查询时未找到可用的索引,进而通过where条件过滤获取所需数据,并不是所有的带where语句的查询都会显示这个.
Using temporary : 查询后结果需要用临时表来存储,一般在排序或者分组查询时用到
Using filesort : 此类型表示无法利用索引完成指定的排序操作,也就是order by的字段实际没有索引,因此此类SQL是需要进行优化的.
边栏推荐
- if,case,for,while
- MySQL Query Exercise (1)
- 七夕节,我用代码制作了表白信封
- "Introduction to nlp + actual combat: Chapter 8: Using Pytorch to realize handwritten digit recognition"
- LeetCode每日一题(2285. Maximum Total Importance of Roads)
- sql注入一般流程(附例题)
- [Medical Insurance Science] To maintain the safety of medical insurance funds, we can do this
- 移动支付线上线下支付场景
- Mockito unit testing
- How class only static allocation and dynamic allocation
猜你喜欢

帮助企业实现数字化转型成功的八项指导原则

The Shell function

系统设计.秒杀系统

SVM介绍以及实战

RSS订阅微信公众号初探-feed43

Basic form validation process

Based on the statistical QDirStat Qt directory

Oracle与Postgresql在PLSQL内事务回滚的重大差异

Gigabit 2 X light 8 electricity management industrial Ethernet switches WEB management - a key Ring Ring net switch
SQL injection in #, - +, - % 20, % 23 is what mean?
随机推荐
【翻译】Terraform和Kubernetes的交集
Mockito unit testing
Power button (LeetCode) 215. The first K largest elements in the array (2022.08.03)
网络工程师入门必懂华为认证体系,附系统学习路线分享
MySQL查询优化与调优
LeetCode每日一题(2285. Maximum Total Importance of Roads)
MRS: Introduction to the use of Alluxio
8.Haproxy 搭建Web集群
2022杭电多校联赛第五场 题解
《nlp入门+实战:第八章:使用Pytorch实现手写数字识别》
打造一份优雅的简历
Hey, I had another fight with HR in the small group!
SQL注入中 #、 --+、 --%20、 %23是什么意思?
内网服务器访问远程服务器的端口映射
FPGA解析B码----连载3
SQL interview Questions
数据治理平台项目总结和分析
XSS related knowledge points
2022年软件测试——精选金融银行面试真题
学会iframe并用其解决跨域问题