现在的位置: 首页 > 综合 > 正文

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

2013年10月14日 ⁄ 综合 ⁄ 共 1263字 ⁄ 字号 评论关闭


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。再加以改善。

    报表例子如下:

   

 

 

 

 

 

 

 

 

 

 

抱歉!评论已关闭.