KingbaseES 查看函数中最耗时的sql
测试
创建测试环境所需表及函数
create table test1(id int);
INSERT INTO test1(id) VALUES (generate_series(1, 10000));
create table test2(id int);
INSERT INTO test2(id) VALUES (generate_series(1, 10000));
create or replace function test_func() returns bigint
as $$
select count(*) from test1;
select count(*) from test2;
select count(*) from test1,test2;
$$
language sql;
\timing on
TEST=# select test_func();
test_func
-----------
100000000
(1 row)
Time: 4313.282 ms (00:04.313)
在函数执行期间,可以查看函数运行情况
TEST=# select pid,usename,substring(query from 0 for 50),now()-query_start as time,wait_event_type,wait_event from pg_stat_activity where state = 'active';
pid | usename | substring | time | wait_event_type | wait_event
-------+---------+---------------------------------------------------+-------------------------------+-----------------+------------
6209 | system | select pid,usename,substring(query from 0 for 50) | +000000000 00:00:00.000000000 | |
10351 | system | select test_func(); | +000000000 00:00:02.713997000 | |
(2 rows)
使用auto_explain显示每个SQL执行计划
加载auto_explain插件
shared_preload_libraries = 'auto_explain'
对于业务非常大的库,不适合全局抓取SQL,可以在客户端开启参数。
设置以下参数,将log_nested_statements = true,展示函数内所有SQL执行计划,当它关闭时,只记录最外层语句的计划,默认值为off
set client_min_messages='log';
set auto_explain.log_min_duration = 0;
set auto_explain.log_analyze = true;
set auto_explain.log_verbose = true;
set auto_explain.log_buffers = true;
set auto_explain.log_nested_statements = true;
以下测试过程,可查看函数中的sql执行时间和执行计划
TEST=# select test_func();
------------- SQL 1执行时间
LOG: duration: 1.825 ms plan:
Query Text:
select count(*) from test1;
select count(*) from test2;
select count(*) from test1,test2;
Aggregate (cost=170.00..170.01 rows=1 width=8) (actual time=1.823..1.823 rows=1 loops=1)
Output: count(*)
Buffers: shared read=45
-> Seq Scan on public.test1 (cost=0.00..145.00 rows=10000 width=0) (actual time=0.019..1.058 rows=10000 loops=1)
Output: id
Buffers: shared read=45
------------- SQL 2执行时间
LOG: duration: 1.876 ms plan:
Query Text:
select count(*) from test1;
select count(*) from test2;
select count(*) from test1,test2;
Aggregate (cost=170.00..170.01 rows=1 width=8) (actual time=1.873..1.874 rows=1 loops=1)
Output: count(*)
Buffers: shared read=45
-> Seq Scan on public.test2 (cost=0.00..145.00 rows=10000 width=0) (actual time=0.016..1.124 rows=10000 loops=1)
Output: id
Buffers: shared read=45
------------- SQL 3执行时间(可以看到占比最大)
LOG: duration: 27710.539 ms plan:
Query Text:
select count(*) from test1;
select count(*) from test2;
select count(*) from test1,test2;
Aggregate (cost=1500315.00..1500315.01 rows=1 width=8) (actual time=27710.531..27710.533 rows=1 loops=1)
Output: count(*)
Buffers: shared hit=90
-> Nested Loop (cost=0.00..1250315.00 rows=100000000 width=0) (actual time=0.015..19808.482 rows=100000000 loops=1)
Buffers: shared hit=90
-> Seq Scan on public.test1 (cost=0.00..145.00 rows=10000 width=0) (actual time=0.007..5.299 rows=10000 loops=1)
Output: test1.id
Buffers: shared hit=45
-> Materialize (cost=0.00..195.00 rows=10000 width=0) (actual time=0.000..0.685 rows=10000 loops=10000)
Buffers: shared hit=45
-> Seq Scan on public.test2 (cost=0.00..145.00 rows=10000 width=0) (actual time=0.004..0.939 rows=10000 loops=1)
Buffers: shared hit=45
------------- 总执行时间
LOG: duration: 27714.881 ms plan:
Query Text: select test_func();
Result (cost=0.00..0.26 rows=1 width=8) (actual time=27714.874..27714.875 rows=1 loops=1)
Output: test_func()
Buffers: shared hit=157 read=96
test_func
-----------
100000000
(1 row)
使用sys_stat_statements_all视图查看函数中select语句耗时
set sys_stat_statements.track="all";
select test_func();
TEST=# select userid::regrole,total_exec_time,query from sys_stat_statements_all order by total_exec_time desc;
userid | total_exec_time | query
--------+--------------------+-----------------------------------------------------------------------------------------------
----------
system | 4151.922228 | select test_func()
system | 4150.4398550000005 | select count(*) from test1,test2
system | 0.569574 | select count(*) from test1
system | 0.510945 | select count(*) from test2
system | 0.108768 | select userid::regrole,total_exec_time,query from sys_stat_statements order by total_exec_time
desc
system | 0 | select userid::regrole,total_exec_time,query from sys_stat_statements_all order by total_exec_
time desc
(6 rows)
使用plprofiler插件也可查看函数内执行时间,内容请参考博客园文档《plprofiler》
https://www.cnblogs.com/kingbase/p/15477503.html
KingbaseES 查看函数中最耗时的sql的更多相关文章
- SqlServer 查看备份文件中逻辑文件信息的Sql语句
RESTORE FILELISTONLY FROM DISK = 'D:\All\DataBase\(2013-12-18)-1.bak' 用来查看备份文件中的逻辑文件信息. 相关信息:SqlServ ...
- SQL集合函数中利用case when then 技巧
我们都知道SQL中适用case when then来转化数据库中的信息 比如 select (case sex when 0 then '男' else '女' end) AS sex from ...
- 【python】dir(__builtins__)查看python中所用BIF(内置函数)
dir(__builtins__)查看python中所用BIF(内置函数)
- 查看Oracle最耗时的SQL
有很多种方法可以用来找出哪些SQL语句需要优化,但是很久以来,最简单的方法都是分析保存在V$SQL视图中的缓存的SQL信息.通过V$SQL视图,可以确定具有高消耗时间.CUP和IO读取的SQL语句. ...
- Oracle 在存储过程或函数中执行字符串sql
有时,我们需要在存储过程或函数中根据条件拼凑一些sql字符串语句,然后再执行拼凑后的sql字符串,如何做到呢? 参考以下代码: FUNCTION CALCULATE_TARGET_SCORE (CUR ...
- 转 SQL集合函数中利用case when then 技巧
SQL集合函数中利用case when then 技巧 我们都知道SQL中适用case when then来转化数据库中的信息 比如 select (case sex when 0 then '男' ...
- 在论坛中出现的比较难的sql问题:19(row_number函数 行转列、sql语句记流水)
原文:在论坛中出现的比较难的sql问题:19(row_number函数 行转列.sql语句记流水) 最近,在论坛中,遇到了不少比较难的sql问题,虽然自己都能解决,但发现过几天后,就记不起来了,也忘记 ...
- 查看dll中的函数(方法)
https://jingyan.baidu.com/article/5553fa82b953b365a23934b7.html 查看dll中的函数(方法) 听语音 | 浏览:2004 | 更新:201 ...
- 查看Oracle中是否有锁表的sql
1.查看是否有锁表的sql 代码如下: select 'blocker('||lb.sid||':'||sb.username||')-sql:'|| qb.sql_text blockers, 'w ...
- vc++6.0中查看函数栈的结构
栈:一种后进先出的数据结构 比如:弹夹 函数调用的约定 传参顺序 传参媒介 如何传递返回值 平衡参数(堆栈平衡):有且只有被调方(callee)和调用方(caller)一方执行 _cdell (c ...
随机推荐
- 【OpenGL ES】FBO离屏渲染
1 前言 OpenGL 默认把 framebuffer 当作渲染目的地,它由窗口系统创建并管理.应用程序也可以创建额外非可显示的 framebuffer object(FBO),以区别窗口系统提供 ...
- Java集合框架学习(十二) Arrays类详解
声明:部分内容参考自:https://liuyanzhao.com/2863.html Arrays类介绍 这个类包含了用于操作数组的各种方法,诸如:排序和搜索. 这个类还包含一个静态方法asList ...
- win32- 使用WM_NCPAINT在非客户区域绘制边框
#pragma comment(lib, "UxTheme") #include <windows.h> #include <uxtheme.h> LRES ...
- 【Android 逆向】【攻防世界】boomshakalaka-3
1. apk 安装到手机,是一个cocos2dx 写的打飞机的游戏 题目描述跟得分有关(题目描述: play the game, get the highest score) 2. jadx 打开ap ...
- 【Android 逆向】【攻防世界】基础android
1. 下载并安装apk,提示要输入密码 2. apk拖入到jadx中看一下 this.login.setOnClickListener(new View.OnClickListener() { // ...
- heapq模块通过nlargest()和nsmallest()找到最大或最小的N个元素
问题 我们想在某个集合中找出最大或最小的N个元素 解决方案 heapq模块中有两个函数nlargest()和nsmallest() import heapq nums = [1,8,2,23,7,-4 ...
- ubantu18.04使用APT安装go环境指令报错解决方案
在ubantu下使用sudo apt install golang-go指令安装go环境,安装过程没有报错,在使用时无法识别指令,报错如下: root@sh001:~# go env -w GOPRO ...
- 【Azure Developer】使用MSAL4J 与 ADAL4J 的SDK时候,遇见了类型冲突问题 "java.util.Collections$SingletonList cannot be cast to java.lang.String"
问题描述 在博文 "[Azure Developer]使用 Powershell az account get-access-token 命令获取Access Token (使用用户名+密码 ...
- Prompt进阶系列1:LangGPT(从编程语言反思LLM的结构化可复用提示设计框架)
Prompt进阶系列1:LangGPT(从编程语言反思LLM的结构化可复用提示设计框架) 大语言模型 (Large Language Models, LLMs) 在不同领域都表现出了优异的性能.然而, ...
- XAF新手入门 - XAF设计模式探讨
前言 刚接触XAF的小伙伴可能会有一个疑惑,XAF中有Model(BusinessObject).View.Controller,感觉明显是一个MVC的设计模式,但当你用MVC的设计模式与其对应时,又 ...