当前位置:网站首页>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是需要进行优化的.
边栏推荐
- Power button (LeetCode) 215. The first K largest elements in the array (2022.08.03)
- 7-3 LVS+Keepalived集群叙述与部署
- Embedded database development programming MySQL (full)
- 高效IO模型
- new Date将字符串转化成日期格式 兼容IE,ie8如何通过new Date将字符串转化成日期格式,js中如何进行字符串替换, replace() 方法详解
- drools从下载到postman请求成功
- 怎么把elastic中的异常登录ip和日志自动导出或抓取到数据库中?
- XSS related knowledge points
- 技术解析|如何将 Pulsar 数据快速且无缝接入 Apache Doris
- The general SQL injection flow (sample attached)
猜你喜欢

数据安全峰会2022 | 美创DSM获颁“数据安全产品能力验证计划”评测证书

本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现

【Ryerson情感说话/歌唱视听数据集(RAVDESS) 】

【项目实现】Boost搜索引擎

软件测试如何系统规划学习呢?

【 observe 】 super fusion: the first mention of "calculate net nine order" evaluation model, build open prosperity of power network

6口全千兆二层网管型工业以太网交换机千兆2光4电光纤自愈ERPS环网交换机
![The video of machine learning to learn [update]](/img/e7/c9a17b4816ce8d4b0787c451520ac3.png)
The video of machine learning to learn [update]

学会iframe并用其解决跨域问题

马尔可夫链
随机推荐
系统设计.秒杀系统
解决问题遇到的问题
本周四晚19:00知识赋能第4期直播丨OpenHarmony智能家居项目之设备控制实现
MySQL query optimization and tuning
【技巧】借助Sentinel实现请求的优先处理
[Ryerson emotional speaking/singing audiovisual dataset (RAVDESS)]
【Ryerson情感说话/歌唱视听数据集(RAVDESS) 】
MRS: Alluxio的使用介绍
类如何只能静态分配和只能动态分配
sql注入一般流程(附例题)
FPGA parsing B code----serial 3
案例 | 重庆银行流动数据安全挑战及应对实践
移动支付线上线下支付场景
七夕节,我用代码制作了表白信封
拿捏JVM性能优化(自己笔记版本)
Mockito unit testing
FFmpeg —— 录制麦克风声音(附源码)
出现504怎么办?由于服务器更新导致的博客报504错误[详细记录]
MySQL 查询练习(1)
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