DC's blog DC's blog
首页
  • 计算机基础
  • linux基础
  • mysql
  • git
  • 数据结构与算法
  • axure
  • english
  • docker
  • opp
  • oop
  • 网络并发编程
  • 不基础的py基础
  • 设计模式
  • html
  • css
  • javascript
  • jquery
  • UI
  • 第一次学vue
  • 第二次学vue
  • Django
  • drf
  • drf_re
  • 温故知新
  • flask
  • 前后端不分离

    • BBS
    • 订单系统
    • CRM
  • 前后端部分分离

    • pear-admin-flask
    • pear-admin-django
  • 前后端分离

    • 供应链系统
  • 理论基础
  • py数据分析包
  • 机器学习
  • 深度学习
  • 华中科大的网课
  • cursor
  • deepseek
  • 杂文
  • 罗老师语录
  • 关于我

    • me
  • 分类
  • 归档
GitHub (opens new window)

DC

愿我一生欢喜,不为世俗所及.
首页
  • 计算机基础
  • linux基础
  • mysql
  • git
  • 数据结构与算法
  • axure
  • english
  • docker
  • opp
  • oop
  • 网络并发编程
  • 不基础的py基础
  • 设计模式
  • html
  • css
  • javascript
  • jquery
  • UI
  • 第一次学vue
  • 第二次学vue
  • Django
  • drf
  • drf_re
  • 温故知新
  • flask
  • 前后端不分离

    • BBS
    • 订单系统
    • CRM
  • 前后端部分分离

    • pear-admin-flask
    • pear-admin-django
  • 前后端分离

    • 供应链系统
  • 理论基础
  • py数据分析包
  • 机器学习
  • 深度学习
  • 华中科大的网课
  • cursor
  • deepseek
  • 杂文
  • 罗老师语录
  • 关于我

    • me
  • 分类
  • 归档
GitHub (opens new window)
  • 计算机基础

  • linux基础

  • mysql

    • 基础

    • 进阶

      • index
      • explain
        • 执行计划
          • ALL
          • INDEX
          • RANGE
          • INDEX_MERGE
          • REF
          • E Q_REF
          • CONST
          • SYSTEM
          • explain 的其它列
      • transaction
      • db_readPhenomenon
      • lock
      • other
  • git

  • 数据结构与算法

  • axure

  • english

  • docker

  • IT_Need
  • mysql
  • 进阶
DC
2022-10-08
目录

explain

# 执行计划

MySQL中提供了执行计划,能够预判SQL的执行(只能给到一定的参考,不一定完全能预判准确)

explain + SQL语句;

其中比较重要的是 type,他他SQL性能比较重要的标志,性能从低到高依次:
all < index < range < index_merge < ref_or_null < ref < eq_ref < system/const

初步判断结果是range及以上 还就OK.

image-20210527074105599

# ALL

全表扫描,数据表从头到尾找一遍。(一般未命中索引,都是会执行全表扫描)

select * from big;

-- 特别的:如果有limit,则找到之后就不在继续向下扫描.
	select * from big limit 1;
1
2
3
4

# INDEX

全索引扫描,对索引从头到尾找一遍

explain select id from big;
explain select name from big;
1
2

# RANGE

对索引列进行范围查找

explain select * from big where id > 10;
explain select * from big where id in (11,22,33);
explain select * from big where id between 10 and 20;
explain select * from big where name > "wupeiqi" ;
1
2
3
4

# INDEX_MERGE

合并索引,使用多个单列索引搜索

-- id是索引 name也是索引
explain select * from big where id = 10 or name="武沛齐";
1
2

# REF

根据 索引 直接去查找(非键)

select *  from big where name = '武沛齐';
1

# E Q_REF

连表操作时常见

explain select big.name,users.id from big left join users on big.age = users.id;
1

# CONST

常量,表最多有一个匹配行,因为仅有一行,在这行的列值可被优化器剩余部分认为是常数,const表很快

explain select * from big where id=11;					-- 主键
explain select * from big where email="[email protected]";	-- 唯一索引
1
2

# SYSTEM

系统,表仅有一行(=系统表)。这是const联接类型的一个特例。

 explain select * from (select * from big where id=1 limit 1) as A;
1

# explain 的其它列

id  查询顺序标识

z  查询类型
    SIMPLE          简单查询
    PRIMARY         最外层查询
    SUBQUERY        映射为子查询
    DERIVED         子查询
    UNION           联合
    UNION RESULT    使用联合的结果
    ...
    
table  正在访问的表名

partitions  涉及的分区(MySQL支持将数据划分到不同的idb文件中,详单与数据的拆分)。 一个特别大的文件拆分成多个小文件(分区)。

1111possible_keys  查询涉及到的字段上若存在索引,则该索引将被列出,即:可能使用的索引。

111111key  显示MySQL在查询中实际使用的索引,若没有使用索引,显示为NULL。例如:有索引但未命中,则possible_keys显示、key则显示NULL。

key_len  表示索引字段的最大可能长度。(类型字节长度 + 变长2 + 可空1),例如:key_len=195,类型varchar(64),195=64*3+2+1

ref  连表时显示的关联信息。例如:A和B连表,显示连表的字段信息。

1111rows  估计读取的数据行数(只是预估值)
  explain select * from big where password ="025dfdeb-d803-425d-9834-445758885d1c";
  explain select * from big where password ="025dfdeb-d803-425d-9834-445758885d1c" limit 1;

filtered  返回结果的行占需要读到的行的百分比。
	explain select * from big where id=1;  -- 100,只读了一个1行,返回结果也是1行。
	explain select * from big where password="27d8ba90-edd0-4a2f-9aaf-99c9d607c3b3";  -- 10,读取了10行,返回了1行。
	注意:密码27d8ba90-edd0-4a2f-9aaf-99c9d607c3b3在第10行
	
extra  该列包含MySQL解决查询的详细信息。
    “Using index”
    此值表示mysql将使用覆盖索引,以避免访问表。不要把覆盖索引和index访问类型弄混了。
    “Using where”
    这意味着mysql服务器将在存储引擎检索行后再进行过滤,许多where条件里涉及索引中的列,当(并且如果)它读取索引时,就能被存储引擎检验,因此不是所有带where子句的查询都会显示“Using where”。有时“Using where”的出现就是一个暗示:查询可受益于不同的索引.
    “Using temporary”
    这意味着mysql在对查询结果排序时会使用一个临时表。
    “Using filesort”
    这意味着mysql会对结果使用一个外部索引排序,而不是按索引次序从表里读取行。mysql有两种文件排序算法,这两种排序方式都可以在内存或者磁盘上完成,explain不会告诉你mysql将使用哪一种文件排序,也不会告诉你排序会在内存里还是磁盘上完成。
    “Range checked for each record(index map: N)”
    这个意味着没有好用的索引,新的索引将在联接的每一行上重新估算,N是显示在possible_keys列中索引的位图,并且是冗余的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
index
transaction

← index transaction→

最近更新
01
deepseek本地部署+知识库
02-17
02
实操-微信小程序
02-14
03
教学-cursor深度探讨
02-13
更多文章>
Theme by Vdoing | Copyright © 2023-2025 DC | One Piece
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式