ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

使用plsql_plprofiler 分析过程块的执行

2021-10-28 20:32:57  阅读:205  来源: 互联网

标签:p1 plsql plprofiler 16485 profiler func test 执行 pl


前言:存储过程可能涉及很多的SQL及控制块,我们看到的执行时间是整个过程块的执行时间,如果我们认为性能有问题,我们只能逐条SQL的分析,查找问题SQL,效率非常低下。KingbaseES 提供了 plsql_plprofiler 扩展插件, 可以方便用户跟踪分析过程的每条语句的执行情况,能让我们快速定位问题。

以下以例子的方式演示plsql_plprofiler的使用。

1、创建扩展插件

create extension plsql_plprofiler

2、创建演示的存储过程

create or replace procedure p1 as
  cnt integer;
begin
  for i in 1..100 loop 
    select count(*) into cnt from t1;
  end loop;
  select count(*) into cnt from t2;
end;
/

3、收集数据

test=# select pl_profiler_reset_local();          --清理本地数据
 pl_profiler_reset_local 
-------------------------
 
(1 row)

test=# select pl_profiler_reset_shared();       --清理全局数据
 pl_profiler_reset_shared 
--------------------------
 
(1 row)

test=# select pl_profiler_set_enabled_local(true);    --启动本地会话分析器
 pl_profiler_set_enabled_local 
-------------------------------
 t
(1 row)

test=# call p1();
CALL
test=# select pl_profiler_set_enabled_local(false);     --关闭本地会话分析器
 pl_profiler_set_enabled_local 
-------------------------------
 f
(1 row)

test=# select pl_profiler_collect_data();
 pl_profiler_collect_data 
--------------------------
                        0
(1 row)

4、分析数据

test=# select pl_profiler_func_oids_shared();
 pl_profiler_func_oids_shared 
------------------------------
 {16485}
(1 row)

test=# select func_oid, func_oid::regproc as funcname,line_number, source from pl_profiler_funcs_source(pl_profiler_func_oids_shared());
 func_oid | funcname | line_number |                source                 
----------+----------+-------------+---------------------------------------
    16485 | p1       |           0 | -- Line 0
    16485 | p1       |           1 | 
    16485 | p1       |           2 |   cnt integer;
    16485 | p1       |           3 | begin
    16485 | p1       |           4 |   for i in 1..100 loop 
    16485 | p1       |           5 |     select count(*) into cnt from t1;
    16485 | p1       |           6 |   end loop;
    16485 | p1       |           7 |   select count(*) into cnt from t2;
    16485 | p1       |           8 | end
(9 rows)

test=# 
test=# SELECT L.func_oid::regproc as funcname,
test-# L.func_oid as func_oid,
test-# L.line_number,
test-# sum(L.exec_count)::bigint AS exec_count,
test-# sum(L.total_time)::bigint AS total_time,
test-# max(L.longest_time)::bigint AS longest_time,
test-# S.source
test-# FROM pl_profiler_linestats_shared() L
test-# JOIN pl_profiler_funcs_source(pl_profiler_func_oids_shared) S
test-# ON S.func_oid = L.func_oid AND S.line_number = L.line_number
test-# GROUP BY L.func_oid, L.line_number, S.source
test-# ORDER BY L.func_oid, L.line_number;
 funcname | func_oid | line_number | exec_count | total_time | longest_time |                source                 
----------+----------+-------------+------------+------------+--------------+---------------------------------------
 p1       |    16485 |           0 |          1 |       1296 |         1296 | -- Line 0
 p1       |    16485 |           1 |          0 |          0 |            0 | 
 p1       |    16485 |           2 |          0 |          0 |            0 |   cnt integer;
 p1       |    16485 |           3 |          1 |       1294 |         1294 | begin
 p1       |    16485 |           4 |          1 |       1098 |         1098 |   for i in 1..100 loop 
 p1       |    16485 |           5 |        100 |        946 |          202 |     select count(*) into cnt from t1;
 p1       |    16485 |           6 |          0 |          0 |            0 |   end loop;
 p1       |    16485 |           7 |          1 |        193 |          193 |   select count(*) into cnt from t2;
 p1       |    16485 |           8 |          0 |          0 |            0 | end
(9 rows)

从收集到的数据可以某条SQL 执行次数、总执行时间。

注意:控制块是包含内部所有语句的执行时间的,如:本例的for 循环

标签:p1,plsql,plprofiler,16485,profiler,func,test,执行,pl
来源: https://www.cnblogs.com/kingbase/p/15477503.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有