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

原文:http://blog.csdn.net/hantiannan/article/details/4513028

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(*)
      FROMpg_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.724ms  plan:

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

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

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

    报表例子如下:

    

 

 

 

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/leeeee/p/7276624.html