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

2022-05-15 0 206
目录
  • 一、概述
    • 1、explain语句测试
    • 2、explain结果中各列的说明
  • 二、explain之id列
    • 1、环境准备
    • 2、explain中的id列详解
  • 三、explain之select_type列
    • 1、select_type
    • 3、PRIMARY、SUBQUERY
    • 4、DERIVED
    • 5、UNION、UNION RESULT
  • 四、explain之table、type列
    • 1、table列
    • 2、type列
    • 3、例
    • (2) system
    • (3) const
    • (4) eq_ref
    • (5) ref
    • (6) index
    • (7) ALL
  • 五、explain之key、rows、extra列
    • 1、key
    • 2、rows
  • 总结

    一、概述

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

    免责声明:
    1、本网站所有发布的源码、软件和资料均为收集各大资源网站整理而来;仅限用于学习和研究目的,您必须在下载后的24个小时之内,从您的电脑中彻底删除上述内容。 不得使用于非法商业用途,不得违反国家法律。否则后果自负!

    2、本站信息来自网络,版权争议与本站无关。一切关于该资源商业行为与www.niceym.com无关。
    如果您喜欢该程序,请支持正版源码、软件,购买注册,得到更好的正版服务。
    如有侵犯你版权的,请邮件与我们联系处理(邮箱:skknet@qq.com),本站将立即改正。

    NICE源码网 MySql MySQL中explain语句的基本使用教程 https://www.niceym.com/36853.html