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 ...
随机推荐
- win32 - ListView_GetItemPosition的使用
ListView_GetItemPosition : Gets the position of a list-view item 理论上获得桌面图标的正确方法是使用shell项,=> IFold ...
- 深入理解Go语言(03):scheduler调度器 - 基本介绍
一:什么是调度 平常我们在生活中会有哪些调度的例子呢?比如十字路口的红绿灯,它就是一种调度系统.在交通十字路口,每个路口上多多少少有一些车辆,为了限制这些车辆不随意行驶,就建起了红绿灯调度系统.红绿灯 ...
- Mysql 插入timestamp没有使用默认值问题
在一次升级过程中,发现Mysql插入数据报了个错 Column 'create_time' cannot be null. 但是看了下这个字段虽然是非null,但是是有默认值的 `create_tim ...
- OpenCV开发笔记(六十六):红胖子8分钟带你总结形态学操作-膨胀、腐蚀、开运算、闭运算、梯度、顶帽、黑帽(图文并茂+浅显易懂+程序源码)
若该文为原创文章,未经允许不得转载原博主博客地址:https://blog.csdn.net/qq21497936原博主博客导航:https://blog.csdn.net/qq21497936/ar ...
- pwd模块
# pwd模块提供了获取UNIX平台用户的账户与密码信息(通过文件/etc/passwd),在所有的UNIX版本平台都可以用. # pwd模块返回的是一个类似元组的对象,该对象的各个属性对应于pass ...
- 在Study.BlazorOne项目中引入Study.Trade模块的实体的表结构
# 1.修改EntityFrameworkCore项目下的BlazorOneDbContext文件,增加一行代码即可 增加Study.Trade.EntityFrameworkCore中的这个方法: ...
- FolkMQ 是怎样进行消息的事务处理?
FolkMQ 提供了二段式提交的事务提交的机制(TCC 模型).允许生产者在发送消息时绑定到一个事务中并接收事务的管理,以确保消息的原子性(要么全成功,要么全失败).在 FolkMQ 中,事务是通过 ...
- 别再低效筛选数据了!试试pandas query函数
数据过滤在数据分析过程中具有极其重要的地位,因为在真实世界的数据集中,往往存在重复.缺失或异常的数据.pandas提供的数据过滤功能可以帮助我们轻松地识别和处理这些问题数据,从而确保数据的质量和准确性 ...
- Redis基础_五大数据类型和常用命令
## 1. Redis基本介绍 1.1 传统数据存储出现的问题 海量用户 高并发 罪魁祸首--关系型数据库: 性能瓶颈:磁盘IO性能低下 扩展瓶颈:数据关系复杂,扩展性差,不便于大规模集群 解决思路 ...
- 17. Class字节码指令解析
## 1. 概述 官方文档:https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-6.html Java 字节码对于虚拟机,就好像汇编语言对于 ...