开发者社区> 问答> 正文

查找最耗费资源的 SQL(Top SQL)


数据库是较大型的应用,对于繁忙的数据库,需要消耗大量的内存、CPU、IO、网络资源。SQL 优化是数据库优化的手段之一,而为了达到 SQL 优化的最佳效果,您首先需要了解最消耗资源的 SQL(Top SQL),例如 IO 消耗最高的 SQL。
数据库资源分为多个维度、CPU、内存、IO 等,为能够从各个维度层面查找最消耗数据库资源的 SQL,您可以使用 pg_stat_statements 插件统计数据库的资源开销和分析 Top SQL。
本文将通过示例介绍如何创建 pg_stat_statements 插件、如何分析 Top SQL 以及如何重置统计信息。

创建 pg_stat_statements 插件


执行如下命令,在需要查询 TOP SQL 的数据库中,创建 pg_stat_statements 插件。

  1. [backcolor=transparent]create extension pg_stat_statements[backcolor=transparent];


分析 TOP SQL



pg_stat_statements 输出内容介绍


通过查询 pg_stat_statements 视图,您可以得到数据库资源开销的统计信息。SQL 语句中的一些过滤条件在 pg_stat_statements 中会被替换成变量,可以减少重复显示的问题。
pg_stat_statements 视图包含了一些重要信息,例如:

  • SQL 的调用次数,总耗时,最快执行时间,最慢执行时间,平均执行时间,执行时间的方差(看出抖动),总共扫描、返回或处理了多少行。

  • shared buffer 的使用情况:命中、未命中、产生脏块、驱逐脏块。

  • local buffer 的使用情况:命中、未命中、产生脏块、驱逐脏块。

  • temp buffer 的使用情况:读了多少脏块、驱逐脏块。

  • 数据块的读写时间。

下表列出了 pg_stat_statements 输出内容中各参数的含义。
NameTypeReferencesDescription
useridoidpg_authid.oidOID of user who executed the statement.
dbidoidpg_database.oidOID of database in which the statement was executed.
queryidbigint-Internal hash code, computed from the statement’s parse tree.
querytext-Text of a representative statement.
callsbigint-Number of times executed.
total_timedouble precision-Total time spent in the statement, in milliseconds.
min_timedouble precision-Minimum time spent in the statement, in milliseconds.
max_timedouble precision-Maximum time spent in the statement, in milliseconds.
mean_timedouble precision-Mean time spent in the statement, in milliseconds.
stddev_timedouble precision-Population standard deviation of time spent in the statement, in milliseconds.
rowsbigint-Total number of rows retrieved or affected by the statement.
shared_blks_hitbigint-Total number of shared block cache hits by the statement.
shared_blks_readbigint-Total number of shared blocks read by the statement.
shared_blks_dirtiedbigint-Total number of shared blocks dirtied by the statement.
shared_blks_writtenbigint-Total number of shared blocks written by the statement.
local_blks_hitbigint-Total number of local block cache hits by the statement.
local_blks_readbigint-Total number of local blocks read by the statement.
local_blks_dirtiedbigint-Total number of local blocks dirtied by the statement.
local_blks_writtenbigint-Total number of local blocks written by the statement.
temp_blks_readbigint-Total number of temp blocks read by the statement.
temp_blks_writtenbigint-Total number of temp blocks written by the statement.
blk_read_timedouble precision-Total time the statement spent reading blocks, in milliseconds (if track_io_timing is enabled, otherwise zero).
blk_write_timedouble precision-Total time the statement spent writing blocks, in milliseconds (if track_io_timing is enabled, otherwise zero).


最耗 IO SQL


执行如下命令,查询单次调用最耗 IO SQL TOP 5。
  1. [backcolor=transparent]select[backcolor=transparent] userid[backcolor=transparent]::[backcolor=transparent]regrole[backcolor=transparent],[backcolor=transparent] dbid[backcolor=transparent],[backcolor=transparent] query [backcolor=transparent]from[backcolor=transparent] pg_stat_statements order [backcolor=transparent]by[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]blk_read_time[backcolor=transparent]+[backcolor=transparent]blk_write_time[backcolor=transparent])/[backcolor=transparent]calls desc limit [backcolor=transparent]5[backcolor=transparent];

执行如下命令,查询总最耗 IO SQL TOP 5。
  1. [backcolor=transparent]select[backcolor=transparent] userid[backcolor=transparent]::[backcolor=transparent]regrole[backcolor=transparent],[backcolor=transparent] dbid[backcolor=transparent],[backcolor=transparent] query [backcolor=transparent]from[backcolor=transparent] pg_stat_statements order [backcolor=transparent]by[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]blk_read_time[backcolor=transparent]+[backcolor=transparent]blk_write_time[backcolor=transparent])[backcolor=transparent] desc limit [backcolor=transparent]5[backcolor=transparent];


最耗时 SQL


执行如下命令,查询单次调用最耗时 SQL TOP 5。
  1. [backcolor=transparent]select[backcolor=transparent] userid[backcolor=transparent]::[backcolor=transparent]regrole[backcolor=transparent],[backcolor=transparent] dbid[backcolor=transparent],[backcolor=transparent] query [backcolor=transparent]from[backcolor=transparent] pg_stat_statements order [backcolor=transparent]by[backcolor=transparent] mean_time desc limit [backcolor=transparent]5[backcolor=transparent];

执行如下命令,查询总最耗时 SQL TOP 5。
  1. [backcolor=transparent]select[backcolor=transparent] userid[backcolor=transparent]::[backcolor=transparent]regrole[backcolor=transparent],[backcolor=transparent] dbid[backcolor=transparent],[backcolor=transparent] query [backcolor=transparent]from[backcolor=transparent] pg_stat_statements order [backcolor=transparent]by[backcolor=transparent] total_time desc limit [backcolor=transparent]5[backcolor=transparent];


响应时间抖动最严重 SQL


执行如下命令,查询响应时间抖动最严重 SQL。
  1. [backcolor=transparent]select[backcolor=transparent] userid[backcolor=transparent]::[backcolor=transparent]regrole[backcolor=transparent],[backcolor=transparent] dbid[backcolor=transparent],[backcolor=transparent] query [backcolor=transparent]from[backcolor=transparent] pg_stat_statements order [backcolor=transparent]by[backcolor=transparent] stddev_time desc limit [backcolor=transparent]5[backcolor=transparent];


最耗共享内存 SQL


执行如下命令,查询最耗共享内存 SQL。
  1. [backcolor=transparent]select[backcolor=transparent] userid[backcolor=transparent]::[backcolor=transparent]regrole[backcolor=transparent],[backcolor=transparent] dbid[backcolor=transparent],[backcolor=transparent] query [backcolor=transparent]from[backcolor=transparent] pg_stat_statements order [backcolor=transparent]by[backcolor=transparent] [backcolor=transparent]([backcolor=transparent]shared_blks_hit[backcolor=transparent]+[backcolor=transparent]shared_blks_dirtied[backcolor=transparent])[backcolor=transparent] desc limit [backcolor=transparent]5[backcolor=transparent];


最耗临时空间 SQL


执行如下命令,查询最耗临时空间 SQL。
  1. [backcolor=transparent]select[backcolor=transparent] userid[backcolor=transparent]::[backcolor=transparent]regrole[backcolor=transparent],[backcolor=transparent] dbid[backcolor=transparent],[backcolor=transparent] query [backcolor=transparent]from[backcolor=transparent] pg_stat_statements order [backcolor=transparent]by[backcolor=transparent] temp_blks_written desc limit [backcolor=transparent]5[backcolor=transparent];


重置统计信息


pg_stat_statements 是累积的统计,如果要查看某个时间段的统计,需要打快照,建议您参见文档 《PostgreSQL AWR报告(for 阿里云ApsaraDB PgSQL)》
您也可以通过执行如下命令,来定期清理历史统计信息。
  1. [backcolor=transparent]select[backcolor=transparent] pg_stat_statements_reset[backcolor=transparent]();

展开
收起
云栖大讲堂 2017-10-18 16:13:58 2016 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
SQL Server 2017 立即下载
GeoMesa on Spark SQL 立即下载
原生SQL on Hadoop引擎- Apache HAWQ 2.x最新技术解密malili 立即下载