可以使用EXPLAIN的ANALYZE选项获取时间的执行计划预估。使用该选项,EXPLAIN会实际的执行查询,然后返回实际的返回行及运行时间。例如:

EXPLAIN ANALYZE SELECT *
FROM tenk1 t1, tenk2 t2
WHERE t1.unique1 < 10 AND t1.unique2 = t2.unique2;
QUERY PLAN
---------------------------------------------------------------------------------------------------------------------------------
Nested Loop (cost=4.65..118.62 rows=10 width=488) (actual time=0.128..0.377 rows=10 loops=1)-> Bitmap Heap Scan on tenk1 t1 (cost=4.36..39.47 rows=10 width=244) (actual time=0.057..0.121 rows=10 loops=1)Recheck Cond: (unique1 < 10)-> Bitmap Index Scan on tenk1_unique1 (cost=0.00..4.36 rows=10 width=0) (actual time=0.024..0.024 rows=10 loops=1)Index Cond: (unique1 < 10)-> Index Scan using tenk2_unique2 on tenk2 t2 (cost=0.29..7.91 rows=1 width=244) (actual time=0.021..0.022 rows=1 loops=10)Index Cond: (unique2 = t1.unique2)
Planning time: 0.181 ms
Execution time: 0.501 ms

请注意,执行时间与成本(cost)的单位是不同的。

在有些执行计划中,某个子计划可能会执行多次。loops会标出执行的次数,实际时间和返回行未每次执行的平均值。

有些情况下,EXLPAIN ANALYZE会展示更多信息。例如,sort和hash节点会提供额外信息:

EXPLAIN ANALYZE SELECT *
FROM tenk1 t1, tenk2 t2
WHERE t1.unique1 < 100 AND t1.unique2 = t2.unique2 ORDER BY
t1.fivethous;
QUERY PLAN
-------------------------------------------------------------------------
Sort (cost=717.34..717.59 rows=101 width=488) (actual time=7.761..7.774 rows=100 loops=1)Sort Key: t1.fivethousSort Method: quicksort Memory: 77kB-> Hash Join (cost=230.47..713.98 rows=101 width=488) (actual time=0.711..7.427 rows=100 loops=1)Hash Cond: (t2.unique2 = t1.unique2)-> Seq Scan on tenk2 t2 (cost=0.00..445.00 rows=10000 width=244) (actual time=0.007..2.583 rows=10000 loops=1)-> Hash (cost=229.20..229.20 rows=101 width=244) (actual time=0.659..0.659 rows=100 loops=1)Buckets: 1024 Batches: 1 Memory Usage: 28kB-> Bitmap Heap Scan on tenk1 t1 (cost=5.07..229.20 rows=101 width=244) (actual time=0.080..0.526 rows=100 loops=1)Recheck Cond: (unique1 < 100)-> Bitmap Index Scan on tenk1_unique1 (cost=0.00..5.04 rows=101 width=0) (actual time=0.049..0.049 rows=100 loops=1)Index Cond: (unique1 < 100)
Planning time: 0.194 ms
Execution time: 8.008 ms

也可以展示被条件剔除掉的行数:

EXPLAIN ANALYZE SELECT * FROM tenk1 WHERE ten < 7;
QUERY PLAN
-------------------------------------------------------------------
Seq Scan on tenk1 (cost=0.00..483.00 rows=7000 width=244) (actual time=0.016..5.107 rows=7000 loops=1)Filter: (ten < 7)Rows Removed by Filter: 3000
Planning time: 0.083 ms
Execution time: 5.905 ms

还可以使用EXPLAIN的BUFFER选项来获得更多运行时状态:

EXPLAIN (ANALYZE, BUFFERS) SELECT * FROM tenk1 WHERE unique1 < 100
AND unique2 > 9000;
QUERY PLAN
--------------------------------------------------------------------------
Bitmap Heap Scan on tenk1 (cost=25.08..60.21 rows=10 width=244) (actual time=0.323..0.342 rows=10 loops=1)Recheck Cond: ((unique1 < 100) AND (unique2 > 9000))Buffers: shared hit=15-> BitmapAnd (cost=25.08..25.08 rows=10 width=0) (actual time=0.309..0.309 rows=0 loops=1)Buffers: shared hit=7-> Bitmap Index Scan on tenk1_unique1 (cost=0.00..5.04 rows=101 width=0) (actual time=0.043..0.043 rows=100 loops=1)Index Cond: (unique1 < 100)Buffers: shared hit=2-> Bitmap Index Scan on tenk1_unique2 (cost=0.00..19.78 rows=999 width=0) (actual time=0.227..0.227 rows=999 loops=1)Index Cond: (unique2 > 9000)Buffers: shared hit=5
Planning time: 0.088 ms
Execution time: 0.423 ms

谨记EXPLAIN ANALYZE会实际执行命令,所以对于DML命令,可以考虑将其放置到事务中,并在执行后回滚,以免对实际数据产生影响:

BEGIN;
EXPLAIN ANALYZE UPDATE tenk1 SET hundred = hundred + 1 WHERE
unique1 < 100;
QUERY PLAN
---------------------------------------------------------------------------------------------
Update on tenk1 (cost=5.07..229.46 rows=101 width=250) (actual time=14.628..14.628 rows=0 loops=1)-> Bitmap Heap Scan on tenk1 (cost=5.07..229.46 rows=101 width=250) (actual time=0.101..0.439 rows=100 loops=1)Recheck Cond: (unique1 < 100)-> Bitmap Index Scan on tenk1_unique1 (cost=0.00..5.04 rows=101 width=0) (actual time=0.043..0.043 rows=100 loops=1)Index Cond: (unique1 < 100)
Planning time: 0.079 ms
Execution time: 14.727 ms
ROLLBACK;

如果UPDATE或DELETE命令影响到继承关系表,那么执行计划:

EXPLAIN UPDATE parent SET f2 = f2 + 1 WHERE f1 = 101;
QUERY PLAN
-----------------------------------------------------------------------------------
Update on parent (cost=0.00..24.53 rows=4 width=14)Update on parentUpdate on child1Update on child2Update on child3-> Seq Scan on parent (cost=0.00..0.00 rows=1 width=14)Filter: (f1 = 101)-> Index Scan using child1_f1_key on child1 (cost=0.15..8.17 rows=1 width=14)Index Cond: (f1 = 101)-> Index Scan using child2_f1_key on child2 (cost=0.15..8.17 rows=1 width=14)Index Cond: (f1 = 101)-> Index Scan using child3_f1_key on child3 (cost=0.15..8.17 rows=1 width=14)Index Cond: (f1 = 101)

14.1.2. EXPLAIN ANALYZE相关推荐

  1. MySQL EXPLAIN ANALYZE

    本文转载自"MySQL解决方案工程师"公众号,由 徐轶韬翻译 作者:Norvald H. Ryeng  译:徐轶韬 MySQL8.0.18刚刚发布,它包含一个全新的功能EXPLAI ...

  2. 根据条件查询某条记录的条数_「性能与架构」MySQL 8 查询优化新工具 Explain Analyze...

    来源:性能与架构公众号 1. Explain Analyze 介绍 Explain 是我们常用的查询分析工具,可以对查询语句的执行方式进行评估,给出很多有用的线索. 但他仅仅是评估,不是实际的执行情况 ...

  3. MySQL的explain analyze增强功能

    我们排查关系型数据库的SQL性能问题时,执行计划是重要的手段之一,看执行计划有很多种方法,Oracle可以参考<查询执行计划的几种方法>.<获取执行计划的方法对比>,MySQL ...

  4. PostgreSQL 10.1 手册_部分 II. SQL 语言_第 14 章 性能提示_14.1. 使用EXPLAIN

    14.1. 使用EXPLAIN 14.1.1. EXPLAIN基础 14.1.2. EXPLAIN ANALYZE 14.1.3. 警告 PostgreSQL为每个收到查询产生一个查询计划. 选择正确 ...

  5. (流式、lambda、触发器)实时处理大比拼 - 物联网(IoT)\金融,时序处理最佳实践

    标签 PostgreSQL , 物联网 , 传感器 , lambda , 调度 , 实时 , 流式更新 , UPSERT , insert on conflict do update 背景 越来越多的 ...

  6. 【转】Parallelism in PostgreSQL

    作者:Percona公司的Ibrar Ahmed https://www.percona.com/blog/2019/07/30/parallelism-in-postgresql/ PostgreS ...

  7. explain的讲解

    在 explain的帮助下,您就知道什么时候该给表添加索引,以使用索引来查找记录从而让select 运行更快. 如果由于不恰当使用索引而引起一些问题的话,可以运行 analyze table来更新该表 ...

  8. 对mysql explain讲的比较清楚的

    对mysql explain讲的比较清楚的 explain结果的每行记录显示了每个表的相关信息,每行记录都包含以下几个字段: id 本次 select 的标识符.在查询中每个 select都有一个顺序 ...

  9. mysql的explain怎么看_mysql中explain用法详解

    如果在select语句前放上关键词explain,mysql将解释它如何处理select,提供有关表如何联接和联接的次序. explain的每个输出行提供一个表的相关信息,并且每个行包括下面的列: 1 ...

最新文章

  1. Object.keys方法之详解
  2. C#二进制格式与文件相互转换
  3. C++性能优化-字符串的优化
  4. Linux入门学习(六)
  5. POJ 3621 Sightseeing Cows [最优比率环]
  6. [VNC] 云服务器 Ubuntu 配置 VNC 遇到的问题
  7. 获取Resources文件下图片的精灵格式
  8. sqlserver2010教程百度云盘_SQLServer数据库基础教程(72集),全套视频教程学习资料通过百度云网盘下载...
  9. Python入门学习—列表(FishC)
  10. 阿里云企业邮箱标准版多域名绑定
  11. java spy_Java Spy - 代码跟踪神器
  12. [鼠标指针][仅需1步]宝藏的猫咪Cat老师[win10/11][点击看更多免费]......
  13. iOS 15 真机调试包 DeviceSupport
  14. JVM学习笔记(13) 垃圾回收-相关概念
  15. 关于滑轮组的计算机知识点,【中考备考】初三物理常考知识点讲解:滑轮
  16. 电信:自娱自乐的全员揽装,让人心寒!
  17. 大学生计算机基础与实训,大学生计算机基础实训六样文.docx
  18. jquery向服务器发送ajax请求标准写法
  19. 希尔排序------排序
  20. Matlab直角坐标系下绘制椭圆面积图

热门文章

  1. java3D实现空间立方体_CSS3 3D旋转立方体
  2. linux conforming code segment nonconforming code segment
  3. OC Protocol(待续)
  4. Python验证码识别:利用pytesser识别简单图形
  5. 排序算法 - 冒泡排序
  6. 在vue中使用3d-force-graph
  7. 【一神】10.15系统 oc引导 amd黑苹果 cpu识别错误/未知
  8. shell脚本入门与Netkeeper破解路由
  9. Intel IPP库概述,合计共5000个函数
  10. 实现保留3位有效数字(四舍六入五成双规则)