当前位置:网站首页>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是需要进行优化的.
边栏推荐
- 张量篇-应用案例
- MRS: Alluxio的使用介绍
- 网络工程师入门必懂华为认证体系,附系统学习路线分享
- 移动支付线上线下支付场景
- The Shell function
- new Date converts strings into date formats Compatible with IE, how ie8 converts strings into date formats through new Date, how to replace strings in js, and explain the replace() method in detail
- 2022年软件测试——精选金融银行面试真题
- 八年软件测试工程师带你了解-测试岗进阶之路
- 《nlp入门+实战:第八章:使用Pytorch实现手写数字识别》
- 元宇宙“吹鼓手”Unity:疯狂扩局,悬念犹存
猜你喜欢
The video of machine learning to learn [update]
数组相关 内容 解析
6-port full Gigabit Layer 2 network managed industrial Ethernet switch Gigabit 2 optical 4 electrical fiber self-healing ERPS ring network switch
复制带随机指针的链表
mysql索引笔记
如果禁用了安全启动,GNOME 就会发出警告
A Preliminary Study of RSS Subscription to WeChat Official Account-feed43
机器学习之视频学习【更新】
tkmapper的crud示例:
[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
随机推荐
6口全千兆二层网管型工业以太网交换机千兆2光4电光纤自愈ERPS环网交换机
Innovation and Integration | Huaqiu Empowerment Helps OpenHarmony Ecological Hardware Development and Landing
深度学习——以CNN服装图像分类为例,探讨怎样评价神经网络模型
Y86. Chapter iv Prometheus giant monitoring system and the actual combat, Prometheus storage (17)
仿牛客论坛项目梳理
什么是数字孪生智慧城市应用场景
7-3 LVS+Keepalived集群叙述与部署
创新互融|华秋赋能助力OpenHarmony生态硬件开发落地
千兆2光8电管理型工业以太网交换机WEB管理X-Ring一键环网交换机
初识Numpy
系统设计.秒杀系统
mq应用场景介绍
MySQL查询优化与调优
Explain详解与实践
Power button (LeetCode) 215. The first K largest elements in the array (2022.08.03)
7-2 LVS+DR概述与部署
SQL interview Questions
XSS related knowledge points
高效IO模型
汇编语言之栈