帮帮忙顶一下这个文章,非常好的一个文章,非常值得了解。
来自:http://blog.chinaunix.net/uID-24774106-ID-3780341.HTML
PostgreSQL部署上之后,经过一段时间的运行,我们比较关心那些sql运行时间比较长,或者说那些sql执行的特别慢,拖累的性能,只有找到这些sql,才能有针对性地对这些sql进行优化,提升Postgresql的性能。
Postgresql提供了pg_stat_statements来存储sql的运行次数,总运行时间,shared_buffer命中次数,shared_buffer read次数等统计信息。
name | Type | References | Description |
---|---|---|---|
userID | oID | pg_authid.oID | OID of user who executed the statement |
dbID | pg_database.oID | OID of database in which the statement was executed | |
query | text | Text of the statement (up totrack_activity_query_sizebytes) | |
calls | bigint | Number of times executed | |
total_time | double precision | Total time spent in the statement,in seconds | |
rows | Total number of rows retrIEved or affected by the statement | ||
shared_blks_hit | bigint | Total number of shared blocks hits by the statement | |
shared_blks_read | Total number of shared blocks reads by the statement | ||
shared_blks_written | Total number of shared blocks writes by the statement | ||
local_blks_hit | Total number of local blocks hits by the statement | ||
local_blks_read | Total number of local blocks reads by the statement | ||
local_blks_written | Total number of local blocks writes by the statement | ||
temp_blks_read | Total number of temp blocks reads by the statement | ||
temp_blks_written | Total number of temp blocks writes by the statemen |
可以看出,pg_stat_statements统计了sql的很多信息,方便我们分析sql的性能。但是这个属于Postgresql的扩展,需要修改postgresql.conf,才能使用:
*** 作步骤如下
1 修改配置文件,并且重启Postgresql方能生效
#--
# PG_STAT_STATEMENTS OPTIONS
#-
shared_preload_librarIEs='pg_stat_statements'
custom_variable_classes'pg_stat_statements'
pg_stat_statements.max=1000
pg_stat_statements.track=all 2 创建pg_stat_statements扩展
CREATEEXTENSION pg_stat_statements; 从此之后,Postgresql就能记录sql的统计信息。
上面的表格虽然丰富,其实我们基本比较关心执行最慢的sql,如何查看执行最慢的10条sql?
SELECT query,calls(total_time/calls)asaveragerows 100.0*shared_blks_hit/nullif(shared_blks_hit+shared_blks_readAShit_percent FROM pg_stat_statements ORDER BYaverageDESCliMIT 10; 我在我本地的DB,查找最慢的2条sql,输出如下:
在我另一台机器上,用pgadmin查看:
统计结果一直都在,重启也不会清零,那么统计结果如何清零重新统计呢?执行下面sql即可:
selectpg_stat_statements_reset(; 找到最耗时的sql,我们就能针对这些耗时的sql,查看是否有优化的余地。
参考文献:
1 More on Postgres Performance
2 Postgresql manual 总结
以上是内存溢出为你收集整理的PostgreSQL之查找最慢的SQL 的方法全部内容,希望文章能够帮你解决PostgreSQL之查找最慢的SQL 的方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)