加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

PostgreSQL SQL的性能调试方法2--数据库log分析

发布时间:2020-12-13 17:57:42 所属栏目:百科 来源:网络整理
导读:1.log_min_duration_statement 从log找出 执行超过一定时间的 SQL。postgresql.conf配置文件 设置 log_min_duration_statement参数的 值。 这个参数是设置执行最小多长时间的 SQL 输出 到 log。 例如 输出执行超过 3秒的SQL:log_min_duration_statement = 3s


1.log_min_duration_statement

从log找出执行超过一定时间的 SQL。postgresql.conf配置文件 设置 log_min_duration_statement参数的 值。

这个参数是设置执行最小多长时间的 SQL 输出log。

例如输出执行超过 3秒的SQL:log_min_duration_statement = 3s
这个参数设置为 -1是无效。 设置为 0是 输出所有的 SQL,但 这样会增加服务器负担,一般不要设置太低的 值。
这样设置后输出的SQL例子如下:
LOG: duration: 3016.724 ms statement: SELECT count(*)
FROM pg_class
2.contrib/auto_explain功能。Postgres8.4后增加的功能。
默认这个功能不能使用的,需要在postgresql.conf 配置文件中设置以下参数。
shared_preload_libraries = 'auto_explain'
custom_variable_classes = 'auto_explain'
auto_explain.log_min_duration = 4s
这样系统在执行的时候如果遇到超过4秒的SQL的话,会自动把执行计划输出到log。这样就直接看log就更加容易找到问题点。

执行计划例子:

LOG: duration: 4016.724 ms plan:

Aggregate (cost=14.90..14.91 rows=1 width=0)
-> Hash Join (cost=3.91..14.70 rows=81 width=0)
Hash Cond: (pg_class.oid = pg_index.indrelid)
-> Seq Scan on pg_class (cost=0.00..8.27 rows=227 width=4)
-> Hash (cost=2.90..2.90 rows=81 width=4)
-> Seq Scan on pg_index (cost=0.00..2.90 rows=81 width=4)
Filter: indisunique
STATEMENT: SELECT count(*)
FROM pg_class,pg_index
WHERE oid = indrelid AND indisunique;
3.log统计分析工具(PostgreSQL log analyzer)

比较有名是pgFouine 。这个工具是自动分析指定的log,然后生成HTML报表。把SQL log图像化后更加直观。

可以统计分析最慢的SQL,调用最多的SQL,花费时间最多的SQL等等分类。这样我们就很容易找到速度慢的SQL。再加以改善。

报表例子如下:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读