服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - MySQL中explain语句的基本使用教程

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

2021-05-03 23:03pedro7 Mysql

这篇文章主要给大家介绍了关于MySQL中explain语句的基本使用教程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

一、概述

 

在 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、

2simple

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使用内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://www.cnblogs.com/WangXianSCU/p/14587085.html

延伸 · 阅读

精彩推荐