利用auto_explain查看sql、procedure、function实时执行计划

news/2024/10/27 22:34:26/

文章目录

  • 1.简介
    • 1.1 实时的执行计划
    • 1.2 查看procedure、function的执行计划
  • 2.load auto_explain
  • 3.相关参数设定
  • 4.创建测试表
  • 5.测试用的function
  • 6.运行测试function
  • 7.查看执行过程
  • 8.关闭auto_explain

1.简介

postgresql中,利用explain 结合一些选项,如analyze、buffers等命令查看sql语句执行计划在一般场景下已经足够,但是基于如下原因,auto_explain将是一个强有力的补充:

1.1 实时的执行计划

鉴于pg优化器的复杂性,以及柱状图不同值数据的分布,sql不总是按照我们使用explain工具看到的执行计划执行

1.2 查看procedure、function的执行计划

对于procedure/function执行计划的分析,通常我们可以raise notice查看各个sql的时间,也可以使用explain一段一段的查看执行计划,但是在procedure/function中往往有者复杂的写法,如for loop循环,要查看这些执行情况变得相当不容易
我们今天将仅演示auto_explain的使用,细节将可以查看postgresql官方文档auto_explain

2.load auto_explain

auto_explain是一个session级的library,可以在使用时动态加载

set session_preload_libraries to auto_explain
load  'auto_explain'

3.相关参数设定

参数具体含义可参考官方文档

set auto_explain.sample_rate = 1
set auto_explain.log_min_duration = 0
set auto_explain.log_analyze = true 
set auto_explain.log_nested_statements to on

4.创建测试表

测试表t1

create table t1 (id bigint)

测试表t2

create table t2 (id bigint)
insert into t2 values(generate_series(1,10000))
create index idx_t2_id on t2(id)

5.测试用的function

create or replace function pro_test1()
returns record
LANGUAGE 'plpgsql'
as $body$
declare
v_num int :=1;
sfc record;
begin 
delete from t1;
for sfc in (select id from t2 where id >9997)
loop
insert into t1(id) select * from t2 where sfc.id=t2.id;
end loop;
return sfc;
end;
$body$;

6.运行测试function

select pro_test1()

7.查看执行过程

2023-06-27 13:53:42.728 WIB,"dbas","pccwms502Zdb",105829,"172.19.6.187:1032",649a5d9e.19d65,15056,"SELECT",2023-06-27 10:55:10 WIB,43/1567572,840415340,LOG,00000,"duration: 0.048 ms  plan:
Query Text: delete from t1
Delete on t1  (cost=0.00..1.05 rows=5 width=6) (actual rows=0 loops=1)->  Seq Scan on t1  (cost=0.00..1.05 rows=5 width=6) (actual rows=5 loops=1)",,,,,"SQL statement ""delete from t1""
PL/pgSQL function pro_test1() line 6 at SQL statement",,,,"pgAdmin 4 - CONN:3952426"
2023-06-27 13:53:42.728 WIB,"dbas","pccwms502Zdb",105829,"172.19.6.187:1032",649a5d9e.19d65,15057,"SELECT",2023-06-27 10:55:10 WIB,43/1567572,840415340,LOG,00000,"duration: 0.019 ms  plan:
Query Text: insert into t1(id) select * from t2 where sfc.id=t2.id
Insert on t1  (cost=0.29..4.31 rows=1 width=4) (actual rows=0 loops=1)->  Index Only Scan using idx_t2_id on t2  (cost=0.29..4.31 rows=1 width=4) (actual rows=1 loops=1)Index Cond: (id = $4)Heap Fetches: 0",,,,,"SQL statement ""insert into t1(id) select * from t2 where sfc.id=t2.id""
PL/pgSQL function pro_test1() line 11 at SQL statement",,,,"pgAdmin 4 - CONN:3952426"
2023-06-27 13:53:42.728 WIB,"dbas","pccwms502Zdb",105829,"172.19.6.187:1032",649a5d9e.19d65,15058,"SELECT",2023-06-27 10:55:10 WIB,43/1567572,840415340,LOG,00000,"duration: 0.009 ms  plan:
Query Text: insert into t1(id) select * from t2 where sfc.id=t2.id
Insert on t1  (cost=0.29..4.31 rows=1 width=4) (actual rows=0 loops=1)->  Index Only Scan using idx_t2_id on t2  (cost=0.29..4.31 rows=1 width=4) (actual rows=1 loops=1)Index Cond: (id = $4)Heap Fetches: 0",,,,,"SQL statement ""insert into t1(id) select * from t2 where sfc.id=t2.id""
PL/pgSQL function pro_test1() line 11 at SQL statement",,,,"pgAdmin 4 - CONN:3952426"
2023-06-27 13:53:42.728 WIB,"dbas","pccwms502Zdb",105829,"172.19.6.187:1032",649a5d9e.19d65,15059,"SELECT",2023-06-27 10:55:10 WIB,43/1567572,840415340,LOG,00000,"duration: 0.007 ms  plan:
Query Text: insert into t1(id) select * from t2 where sfc.id=t2.id
Insert on t1  (cost=0.29..4.31 rows=1 width=4) (actual rows=0 loops=1)->  Index Only Scan using idx_t2_id on t2  (cost=0.29..4.31 rows=1 width=4) (actual rows=1 loops=1)Index Cond: (id = $4)Heap Fetches: 0",,,,,"SQL statement ""insert into t1(id) select * from t2 where sfc.id=t2.id""
PL/pgSQL function pro_test1() line 11 at SQL statement",,,,"pgAdmin 4 - CONN:3952426"
2023-06-27 13:53:42.729 WIB,"dbas","pccwms502Zdb",105829,"172.19.6.187:1032",649a5d9e.19d65,15060,"SELECT",2023-06-27 10:55:10 WIB,43/1567572,840415340,LOG,00000,"duration: 0.010 ms  plan:
Query Text: (select id from t2 where id >9997)
Index Only Scan using idx_t2_id on t2  (cost=0.29..4.34 rows=3 width=8) (actual rows=3 loops=1)Index Cond: (id > 9997)Heap Fetches: 0",,,,,"PL/pgSQL function pro_test1() line 8 at FOR over SELECT rows",,,,"pgAdmin 4 - CONN:3952426"
2023-06-27 13:53:42.729 WIB,"dbas","pccwms502Zdb",105829,"172.19.6.187:1032",649a5d9e.19d65,15061,"SELECT",2023-06-27 10:55:10 WIB,43/1567572,840415340,LOG,00000,"duration: 1.075 ms  plan:
Query Text: select pro_test1()
Result  (cost=0.00..0.26 rows=1 width=32) (actual rows=1 loops=1)",,,,,,,,,"pgAdmin 4 - CONN:3952426"

这里,可以看到输出的完整执行计划,甚至每一个for循环的执行计划都有记录下来,这对我们查看复杂sql的执行计划非常的有帮助

8.关闭auto_explain

set autoexplain.log_min_duration to -1

http://www.ppmy.cn/news/702067.html

相关文章

耳机插入电脑插孔后只有一边有声音

耳机插入电脑插孔后只有一边有声音, 解决方法: 搜索并打开音频管理器: Realtek HD Audio Manager 检查立体声道是否正常,如果正常,问题可能出在音频播放软件。可以再软件声音设置中修复。

Windows10主机插入耳机只有一边有声音

Windows10主机插入耳机只有一边有声音 在网上看了好几个版本,排除了主机插孔和耳机本身的问题,根据一篇文章在声音设置中找到了答案,原文章不是windows10,所以我找了好一会才找到,所以特地写出来供其他windows10用户参…

小米air2se耳机只有一边有声音怎么办_小米Air2 se评测,对比有线,无线,真无线,到底哪个更适合你...

本文主要从连接,佩戴,音质以及续航四个方面对比小米AIr2 se,小米项圈降噪蓝牙耳机,小米圈铁pro 连接 1,速度:圈铁pro>2se>降噪有限肯定是最快的,air2se支持弹窗&#xff…

【C#】并行编程实战:实现数据并行(4)

本章继续学习实现数据并行,本文主要介绍并行循环中的线程存储。这也是本章节的最后一篇。 本教程对应学习工程:魔术师Dix / HandsOnParallelProgramming GitCode 5、了解并行循环中的线程存储 默认情况下,所有并行循环都可以访问全局…

Kingbase人大金仓数据库安装

1.进入人大金仓官网进项下载安装包和授权文件 2.运行下载的安装包进行安装 3.授权文件时,添加下载的授权文件 4.安装目录可根据自己情况修改 5.初始化数据库,账号密码按自己爱好输入,数据库兼容按照自己项目进行兼容 6.完成安装&#xff0…

百度账号 不用手机号注册

百度云 不用手机号注册 CreateTime--2018年5月2日08:50:22 Author:Marydon 1.说明: 间接实现:现在注册时必须使用手机号注册,但是,我们可以注册成功后,绑定邮箱后再解绑手机号。 2.实现 如果已经有一个账号并且已经绑定…

注册邮箱为什么都要手机号?

这里写自定义目录标题 想注册一个邮箱使用,找了新浪,网易,搜狐三大邮箱网站,发现都需要手机号才可以注册,我就很奇怪了,注册邮箱,为什么要我的手机号呢? 难道是我免费用你的邮箱&…

手机号码短信验证注册

手机号码短信验证 我用的是id编辑器开发 用的技术是ssm 去互亿无线注册一个账号获取手机短信验证下载一个短信验证的api文档打开里面的java/http_post/lib四个插件和java/http_post/src里面的工具类后端代码如下 3. private static String Url "http://106.ihuyi.com/w…