数据库教程:MySQL中explain语句的基本使用教程

一、概述在 mysql 中,我们可以使用慢查询日志或者 show processlist 命令等方式定位到执行耗时较长的 sql 语句,在这之后我们可以通过 explain或者 desc 命令获取 m


一、概述

在 mysql 中,我们可以使用慢查询日志或者 show processlist 命令等方式定位到执行耗时较长的 sql 语句,在这之后我们可以通过 explain或者 desc 命令获取 mysql 如何执行 select 语句 的信息,包括在 select 语句执行过程中表如何连接和连接的顺序

1、explain语句测试

explain+select语句即 explain 命令的使用方式

MySQL中explain语句的基本使用教程

2、explain结果中各列的说明

MySQL中explain语句的基本使用教程

下面,将对这些列逐一进行讲解

二、explain之id列

1、环境准备

MySQL中explain语句的基本使用教程
MySQL中explain语句的基本使用教程

2、explain中的id列详解

id 字段是 select 查询的序列号,是一组数字,表示的是查询中执行 select 子句或者是操作表的顺序。id 情况有三种
(1) id 相同表示加载表的顺序是从上到下

MySQL中explain语句的基本使用教程

(2) id 不同id值越大,优先级越高,越先被执行

MySQL中explain语句的基本使用教程

(3) id 有相同,也有不同,同时存在。id相同的可以认为是一组,从上往下顺序执行;在所有的组中,id的值越大,优先级越高,越先执行。

MySQL中explain语句的基本使用教程

三、explain之select_type列

1、select_type

MySQL中explain语句的基本使用教程2、

simple

MySQL中explain语句的基本使用教程

3、primary、subquery

MySQL中explain语句的基本使用教程

4、derived

MySQL中explain语句的基本使用教程

5、union、union result

MySQL中explain语句的基本使用教程

四、explain之table、type列

1、table列

table列展示该行数据属于哪张表

2、type列

type列显示的是访问类型

MySQL中explain语句的基本使用教程

3、例

(1) null

MySQL中explain语句的基本使用教程

(2) system

MySQL中explain语句的基本使用教程

(3) const

MySQL中explain语句的基本使用教程

(4) eq_ref

MySQL中explain语句的基本使用教程

(5) ref

MySQL中explain语句的基本使用教程

(6) index

MySQL中explain语句的基本使用教程

(7) all

MySQL中explain语句的基本使用教程

五、explain之key、rows、extra列

1、key

MySQL中explain语句的基本使用教程
MySQL中explain语句的基本使用教程

2、rows

MySQL中explain语句的基本使用教程

扫描行的数量

MySQL中explain语句的基本使用教程

如果有索引,就只扫描一行

MySQL中explain语句的基本使用教程

3、extra

MySQL中explain语句的基本使用教程

MySQL中explain语句的基本使用教程

总结

到此这篇关于mysql中explain语句的基本使用教程的文章就介绍到这了,更多相关mysql中explain使用内容请搜索<计算机技术网(www.ctvol.com)!!>以前的文章或继续浏览下面的相关文章希望大家以后多多支持<计算机技术网(www.ctvol.com)!!>!

需要了解更多数据库技术:MySQL中explain语句的基本使用教程,都可以关注数据库技术分享栏目—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/dtteaching/808904.html

(0)
上一篇 2021年9月11日
下一篇 2021年9月11日

精彩推荐