当前位置: 首页 > news >正文

PostgreSQL执行计划获取与修改

目录:

      • 基础操作之___执行计划获取与修改
        • 1、查询sql执行计划
          • (1)查询执行计划(不执行)
          • (2)查询执行计划(实际执行)
        • 2、获取执行计划IO信息
        • 3、修改SQL执行计划
          • (1)查询表(无主键索引)
          • (2)查询表(有主键索引)
          • (3)enable_indexscan参数
          • (4)enable_bitmapscan参数

基础操作之___执行计划获取与修改

说明:

  • explain + sql:只显示执行计划,不实际执行
  • explain analyze + sql:实际执行,并显示执行计划

1、查询sql执行计划

启用sql执行时间

htdb=# \timing
Timing is on.
(1)查询执行计划(不执行)
htdb=# explain select count(1) from httab;
                           QUERY PLAN                            
-----------------------------------------------------------------
 Aggregate  (cost=219.00..219.01 rows=1 width=8)
   ->  Seq Scan on httab  (cost=0.00..194.00 rows=10000 width=0)
(2 rows)

Time: 8.421 ms
(2)查询执行计划(实际执行)
htdb=# explain analyze select count(1) from httab;
                                                  QUERY PLAN                                                   
---------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=219.00..219.01 rows=1 width=8) (actual time=2.767..2.768 rows=1 loops=1)
   ->  Seq Scan on httab  (cost=0.00..194.00 rows=10000 width=0) (actual time=0.747..2.450 rows=10000 loops=1)
 Planning Time: 0.041 ms
 Execution Time: 2.790 ms
(4 rows)

Time: 3.073 ms

通过用时,和统计信息两种方式,都能看出二者差别

2、获取执行计划IO信息

使用buffers参数显示sql执行时IO相关信息

htdb=# explain (analyze,buffers) select count(1) from httab;
                                                  QUERY PLAN                                                   
---------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=219.00..219.01 rows=1 width=8) (actual time=0.929..0.929 rows=1 loops=1)
   Buffers: shared hit=94
   ->  Seq Scan on httab  (cost=0.00..194.00 rows=10000 width=0) (actual time=0.006..0.553 rows=10000 loops=1)
         Buffers: shared hit=94
 Planning Time: 0.041 ms
 Execution Time: 0.945 ms
(6 rows)

Time: 1.310 ms
htdb=# 

注意:explain后跟多个选项时,需要加括号

hit表示命中缓存,重启后缓存消失,所以变成物理读(read),如下:

htdb=# \! pg_ctl restart -D /pgdata12
waiting for server to shut down.... done
server stopped
waiting for server to start....2022-08-25 15:40:50.392 CST [10128] LOG:  starting PostgreSQL 12.3 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-36), 64-bit
2022-08-25 15:40:50.393 CST [10128] LOG:  listening on IPv4 address "0.0.0.0", port 5432
2022-08-25 15:40:50.393 CST [10128] LOG:  listening on IPv6 address "::", port 5432
2022-08-25 15:40:50.393 CST [10128] LOG:  listening on Unix socket "/tmp/.s.PGSQL.5432"
2022-08-25 15:40:50.404 CST [10128] LOG:  redirecting log output to logging collector process
2022-08-25 15:40:50.404 CST [10128] HINT:  Future log output will appear in directory "log".
 done
server started
htdb=# \c
You are now connected to database "htdb" as user "htuser".
htdb=# 
htdb=# explain (analyze,buffers) select count(1) from httab;
                                                  QUERY PLAN                                                   
---------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=219.00..219.01 rows=1 width=8) (actual time=2.696..2.696 rows=1 loops=1)
   Buffers: shared read=94
   ->  Seq Scan on httab  (cost=0.00..194.00 rows=10000 width=0) (actual time=0.006..2.246 rows=10000 loops=1)
         Buffers: shared read=94
 Planning Time: 0.255 ms
 Execution Time: 2.739 ms
(6 rows)

Time: 3.804 ms
htdb=#

3、修改SQL执行计划

pg中通过修改执行计划选项开关来修改执行计划

(1)查询表(无主键索引)

没有主键,走的全表扫描

htdb=# explain (analyze,buffers) select * from httab where id =998;
                                            QUERY PLAN                                            
--------------------------------------------------------------------------------------------------
 Seq Scan on httab  (cost=0.00..219.00 rows=1 width=41) (actual time=0.050..0.456 rows=1 loops=1)
   Filter: (id = 998)
   Rows Removed by Filter: 9999
   Buffers: shared hit=94
 Planning Time: 0.053 ms
 Execution Time: 0.464 ms
(6 rows)

(2)查询表(有主键索引)

指定索引列条件,执行计划走index scan

htdb=# ALTER TABLE httab ADD CONSTRAINT httab_pkey PRIMARY KEY (id); 
ALTER TABLE

htdb=#  explain (analyze,buffers) select * from httab where id =998;
                                                    QUERY PLAN                                                     
-------------------------------------------------------------------------------------------------------------------
 Index Scan using httab_pkey on httab  (cost=0.29..8.30 rows=1 width=41) (actual time=0.445..0.447 rows=1 loops=1)
   Index Cond: (id = 998)
   Buffers: shared hit=1 read=2
 Planning Time: 0.824 ms
 Execution Time: 0.461 ms
(5 rows)

Time: 1.623 ms
htdb=#
(3)enable_indexscan参数

禁用enable_indexscan后,查询走bitmap heap scan

htdb=# set enable_indexscan = off;
SET
htdb=# explain (analyze,buffers) select * from httab where id =998;
                                                    QUERY PLAN                                                     
-------------------------------------------------------------------------------------------------------------------
 Bitmap Heap Scan on httab  (cost=4.29..8.31 rows=1 width=41) (actual time=0.009..0.010 rows=1 loops=1)
   Recheck Cond: (id = 998)
   Heap Blocks: exact=1
   Buffers: shared hit=1 read=2
   ->  Bitmap Index Scan on httab_pkey  (cost=0.00..4.29 rows=1 width=0) (actual time=0.007..0.007 rows=1 loops=1)
         Index Cond: (id = 998)
         Buffers: shared read=2
 Planning Time: 0.164 ms
 Execution Time: 0.021 ms
(9 rows)

htdb=# 
(4)enable_bitmapscan参数

禁用enable_bitmapscan后,查询再次走全表扫描

htdb=# explain (analyze,buffers) select * from httab where id =998;
                                            QUERY PLAN                                            
--------------------------------------------------------------------------------------------------
 Seq Scan on httab  (cost=0.00..219.00 rows=1 width=41) (actual time=0.055..0.463 rows=1 loops=1)
   Filter: (id = 998)
   Rows Removed by Filter: 9999
   Buffers: shared hit=94
 Planning Time: 0.045 ms
 Execution Time: 0.472 ms
(6 rows)

auto_explain插件,能够把执行时间较长的SQL及执行计划写到PG日志里

查看执行计划的网站:https://explain.depesz.com/

相关文章:

  • 大幅提升CLIP图像分类准确率-Tip-Adapter
  • CCF- CSP历年认证考试题目链接+题解总结(持续更新)
  • 泛型、IO流 和 网络编程
  • 这些Java基础知识,诸佬们都还记得嘛(学习,复习,面试都可)
  • SSM《程序设计基础》课程答疑系统的设计与实现 毕业设计-附源码261620
  • R语言使用order函数按照两个数据列的值排序data.table数据(主变量升序排序、次变量降序排序)
  • Java Web 2 数据库 2.1 数据库相关概念 2.2 常见的关系型数据库管理系统 2.3 MySQL 数据库
  • DP58 红和蓝
  • grafana+prometheus+(采集节点)实现监控Linux服务器,JVM,Postgres
  • Unity 之 发布字节抖音小游戏
  • Web配置过滤器,Cookie对象的简单使用
  • 进程入门与PCB基础知识.
  • 【云原生】设备云之基于FlexManager的C#SDK开发案例代码
  • Rust(7):数组类型
  • STM32——FLASH闪存编程原理与步骤
  • [iOS]Core Data浅析一 -- 启用Core Data
  • 【162天】黑马程序员27天视频学习笔记【Day02-上】
  • CentOS 7 防火墙操作
  • ES2017异步函数现已正式可用
  • mysql 5.6 原生Online DDL解析
  • php ci框架整合银盛支付
  • thinkphp5.1 easywechat4 微信第三方开放平台
  • 大整数乘法-表格法
  • 动态规划入门(以爬楼梯为例)
  • 开放才能进步!Angular和Wijmo一起走过的日子
  • 如何设计一个微型分布式架构?
  • 入口文件开始,分析Vue源码实现
  • 深度解析利用ES6进行Promise封装总结
  • 使用 @font-face
  • 微信支付JSAPI,实测!终极方案
  • 转载:[译] 内容加速黑科技趣谈
  • Android开发者必备:推荐一款助力开发的开源APP
  • hi-nginx-1.3.4编译安装
  • ​ 轻量应用服务器:亚马逊云科技打造全球领先的云计算解决方案
  • ​如何使用ArcGIS Pro制作渐变河流效果
  • # MySQL server 层和存储引擎层是怎么交互数据的?
  • #13 yum、编译安装与sed命令的使用
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • (4) openssl rsa/pkey(查看私钥、从私钥中提取公钥、查看公钥)
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (LeetCode 49)Anagrams
  • (附源码)springboot家庭装修管理系统 毕业设计 613205
  • (附源码)springboot课程在线考试系统 毕业设计 655127
  • (附源码)ssm基于web技术的医务志愿者管理系统 毕业设计 100910
  • (学习日记)2024.04.04:UCOSIII第三十二节:计数信号量实验
  • (已解决)vue+element-ui实现个人中心,仿照原神
  • (转)http协议
  • .NET BackgroundWorker
  • .NET 分布式技术比较
  • .NET多线程执行函数
  • /etc/sudoers (root权限管理)
  • @RunWith注解作用
  • @四年级家长,这条香港优才计划+华侨生联考捷径,一定要看!
  • [Angular] 笔记 21:@ViewChild
  • [Arduino学习] ESP8266读取DHT11数字温湿度传感器数据