使用pg_stat_statements抓取数据库TOP SQL

文章目录

环境

系统平台:Linux x86-64 Red Hat Enterprise Linux 7,银河麒麟 (X86_64)

版本:9.0,9.0.3,9.0.4

文档用途

版本说明

  • 本文涉及的SQL脚本适用于pg_stat_statements 1.9以后,即HGDB v9以后的版本,查看插件版本可使用psql登陆后执行\dx查看。
  • 本文不涉及插件pg_stat_statements的部署说明。

详细信息

使用说明

  • 插件安装完毕以后,pg_stat_statements表中没有时间戳列,因此采集的数据起始时间为安装插件的时间,采集的数据为安装插件起的累计数据。
  • 如果有定向采集的需求,建议在执行查询pg_stat_statements前手动重置pg_stat_statements的记录,并等待采集一段时间后执行查询命令。

重置pg_stat_statements记录

sql 复制代码
SELECT pg_stat_statements_reset();

查询执行时间的TOP SQL

sql 复制代码
SELECT 
   substring(query,1,50), 
   queryid,
    calls,
    total_exec_time,
    ROUND(CAST(100.0 * total_exec_time / NULLIF(SUM(total_exec_time) OVER (), 0) AS numeric), 2) AS percentage_total_time,
    ROUND(CAST(total_exec_time / NULLIF(calls, 0) AS numeric), 2) AS avg_time_per_call
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 20;

                     substring                      |       queryid        | calls |  total_exec_time   | percentage_total_time | avg_time_per_call
----------------------------------------------------+----------------------+-------+--------------------+-----------------------+-------------------
 SELECT take_sample()                               | -1885067886343922859 |     1 |          957.25066 |                 33.30 |            957.25
 SELECT * FROM take_sample_subset(1,0)              |  4279534554688483984 |     1 |  957.1010279999999 |                 33.30 |            957.10
 UPDATE last_stat_tables ulst                      +|   980343821264315606 |     1 |         706.703659 |                 24.59 |            706.70
     SET in_sample = $                              |                      |       |                    |                       |
 INSERT INTO last_stat_indexes(                    +| -7952618813933756158 |     2 |          32.812992 |                  1.14 |             16.41
         server_id,                                +|                      |       |                    |                       |
                                                    |                      |       |                    |                       |
 INSERT INTO last_stat_tables(                     +| -4538647355087417803 |     2 |          27.614337 |                  0.96 |             13.81
         server_id,                                +|                      |       |                    |                       |
                                                    |                      |       |                    |                       |
 SELECT collect_pg_stat_statements_stats(server_pro |  4567360260458699963 |     1 |          27.481461 |                  0.96 |             27.48
 SELECT num_nulls(min(s.sample_id),max(s.sample_id) |  7475033235738850795 |     1 |          17.213193 |                  0.60 |             17.21
 DELETE FROM samples dsmp                          +|  -154048289740345458 |     1 |          15.040277 |                  0.52 |             15.04
   USING                                           +|                      |       |                    |                       |
     servers srv                                   +|                      |       |                    |                       |
                                                    |                      |       |                    |                       |
 SELECT st.relid,st.schemaname,st.relname,st.seq_sc | -3629045378463163003 |     1 |          12.123222 |                  0.42 |             12.12
 UPDATE last_stat_tables ulst                      +| -5907021954271659257 |     1 |            9.78708 |                  0.34 |              9.79
     SET in_sample = $                              |                      |       |                    |                       |
 UPDATE last_stat_indexes uli                      +|    14458817000285957 |     1 |           8.935439 |                  0.31 |              8.94
     SET in_sample = $                              |                      |       |                    |                       |
 UPDATE last_stat_tables ulst                      +|   603590958747687395 |     1 |           6.345096 |                  0.22 |              6.35
     SET in_sample = $                              |                      |       |                    |                       |
 SELECT save_pg_stat_statements(sserver_id, s_id,  +| -2680229593466005629 |     1 |           6.225859 |                  0.22 |              6.23
                                                    |                      |       |                    |                       |
 INSERT INTO sample_stat_tables (                  +|  3522316392995680077 |     1 |           6.038669 |                  0.21 |              6.04
       server_id,                                  +|                      |       |                    |                       |
                                                    |                      |       |                    |                       |
 UPDATE last_stat_indexes cur                      +| -3677961896277714985 |     1 |           5.474875 |                  0.19 |              5.47
     SET relsize = lst                              |                      |       |                    |                       |
 INSERT INTO sample_stat_tables_total(             +|  3227103508814905323 |     1 |  4.941876000000001 |                  0.17 |              4.94
       server                                       |                      |       |                    |                       |
 ANALYZE last_stat_tables_srv1                      |  4355419411286857634 |     1 |           4.582182 |                  0.16 |              4.58
 SELECT dblink_connect($9,db_connstr)               |  7643119528241000382 |     2 |  4.570225000000001 |                  0.16 |              2.29
 UPDATE last_stat_tables cur                       +|   322557813175172263 |     1 | 4.3211129999999995 |                  0.15 |              4.32
     SET relsize = lst.                             |                      |       |                    |                       |
 SELECT $4 FROM ONLY "public"."sample_stat_database |  4289266549190127233 |  2945 |  4.097789999999995 |                  0.14 |              0.00
(20 rows)
  • calls:sql被调用次数。
  • total_exec_time:sql总执行时间ms=calls*avg_time_per_call。
  • percentage_total_time:总执行时间的占比%。
  • avg_time_per_call:sql平均执行时间,单位ms。

查询占用CPU使用率的TOP SQL

sql 复制代码
select queryid,userid::regrole 用户名, (select datname from pg_database where oid=dbid)数据库,calls as 总调用次数,round((100 * total_exec_time / sum(total_exec_time::numeric) over ())::numeric, 2) as cpu使用率,substring(query,1,50) 查询语句 from  pg_stat_statements order by total_exec_time desc limit 20;

       queryid        | 用户名 | 数据库 | 总调用次数 | cpu使用率 |                      查询语句
----------------------+--------+--------+------------+-----------+----------------------------------------------------
 -1885067886343922859 | highgo | tpcc   |          1 |     33.29 | SELECT take_sample()
  4279534554688483984 | highgo | tpcc   |          1 |     33.29 | SELECT * FROM take_sample_subset(1,0)
   980343821264315606 | highgo | tpcc   |          1 |     24.58 | UPDATE last_stat_tables ulst                      +
                      |        |        |            |           |     SET in_sample = $
 -7952618813933756158 | highgo | tpcc   |          2 |      1.14 | INSERT INTO last_stat_indexes(                    +
                      |        |        |            |           |         server_id,                                +
                      |        |        |            |           |
 -4538647355087417803 | highgo | tpcc   |          2 |      0.96 | INSERT INTO last_stat_tables(                     +
                      |        |        |            |           |         server_id,                                +
                      |        |        |            |           |
  4567360260458699963 | highgo | tpcc   |          1 |      0.96 | SELECT collect_pg_stat_statements_stats(server_pro
  7475033235738850795 | highgo | tpcc   |          1 |      0.60 | SELECT num_nulls(min(s.sample_id),max(s.sample_id)
  -154048289740345458 | highgo | tpcc   |          1 |      0.52 | DELETE FROM samples dsmp                          +
                      |        |        |            |           |   USING                                           +
                      |        |        |            |           |     servers srv                                   +
                      |        |        |            |           |
 -3629045378463163003 | highgo | highgo |          1 |      0.42 | SELECT st.relid,st.schemaname,st.relname,st.seq_sc
 -5907021954271659257 | highgo | tpcc   |          1 |      0.34 | UPDATE last_stat_tables ulst                      +
                      |        |        |            |           |     SET in_sample = $
    14458817000285957 | highgo | tpcc   |          1 |      0.31 | UPDATE last_stat_indexes uli                      +
                      |        |        |            |           |     SET in_sample = $
   603590958747687395 | highgo | tpcc   |          1 |      0.22 | UPDATE last_stat_tables ulst                      +
                      |        |        |            |           |     SET in_sample = $
 -2680229593466005629 | highgo | tpcc   |          1 |      0.22 | SELECT save_pg_stat_statements(sserver_id, s_id,  +
                      |        |        |            |           |
  3522316392995680077 | highgo | tpcc   |          1 |      0.21 | INSERT INTO sample_stat_tables (                  +
                      |        |        |            |           |       server_id,                                  +
                      |        |        |            |           |
 -3677961896277714985 | highgo | tpcc   |          1 |      0.19 | UPDATE last_stat_indexes cur                      +
                      |        |        |            |           |     SET relsize = lst
  3227103508814905323 | highgo | tpcc   |          1 |      0.17 | INSERT INTO sample_stat_tables_total(             +
                      |        |        |            |           |       server
  4355419411286857634 | highgo | tpcc   |          1 |      0.16 | ANALYZE last_stat_tables_srv1
  7643119528241000382 | highgo | tpcc   |          2 |      0.16 | SELECT dblink_connect($9,db_connstr)
   322557813175172263 | highgo | tpcc   |          1 |      0.15 | UPDATE last_stat_tables cur                       +
                      |        |        |            |           |     SET relsize = lst.
  4289266549190127233 | highgo | tpcc   |       2945 |      0.14 | SELECT $4 FROM ONLY "public"."sample_stat_database
(20 rows)
  • CPU使用率:为该SQL总调用次数的CPU使用率,平均单条SQL CPU使用率=CPU使用率/总调用次数。
相关推荐
yanwumuxi4 小时前
【数据库系列】opensearch数据备份和恢复
数据库
anxiao_m4 小时前
2026企业AI数字孪生选型攻略,不同场景对应不同解决方案
大数据·网络·数据库
SelectDB4 小时前
从 ClickHouse 迁移到 Doris:SQL 兼容、同步与验证清单(实战笔记)
大数据·数据库·数据分析
SelectDB4 小时前
Doris 还是 ClickHouse?一张表说清 6 个关键差异(实战笔记)
大数据·数据库·数据分析
oradh5 小时前
Oracle Undo问题总结(两类Undo表空间不足和单个会话占用大量Undo的性能问题)
数据库·oracle
企业数字化笔记5 小时前
固定资产财务账与实物账怎么对账?折旧快照、差异检测与SQL核对
android·数据库·sql
DBA_G5 小时前
GBase 8a表空间多路径存储数据迁移链路适配解析
数据库·oracle
java1234_小锋5 小时前
Tornado 6.5,全面支持 Python 3.14
数据库·python·tornado
jyOverQ5 小时前
MySQL 事务到底是什么?ACID 四大特性与隔离级别怎么理解?
数据库·mysql