您的位置:首页技术文章
文章详情页

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

【字号: 日期:2023-10-04 12:28:43浏览:3作者:猪猪
一、概述

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

标签: MySQL 数据库
相关文章: