postgres定位执行慢的SQL的方法

来源:互联网 发布:算法研究员是做什么 编辑:程序博客网 时间:2024/06/05 03:14
定位慢查询SQL 
可以设置一定时长的参数(log_min_duration_statement),来记录超过该时长的所有SQL,对找出当前数据库的慢查询很有效。 比如log_min_duration_statement = 2s,记录超过2秒的SQL,改完需要reload 


示例:
postgres=# show log_min_duration_statement ;
 log_min_duration_statement 
----------------------------
 2s
(1 row)


postgres=# \timing 
Timing is on.
postgres=# select now(),pg_sleep(1);
             now              | pg_sleep 
------------------------------+----------
 2013-03-29 12:36:48.13353-07 | 
(1 row)


Time: 1001.844 ms
postgres=# select now(),pg_sleep(4);
              now              | pg_sleep 
-------------------------------+----------
 2013-03-29 12:36:28.309595-07 | 
(1 row)


Time: 4002.273 ms


[postgres@localhost pg_log]$ tail -f postgresql-2013-03-29_000000.csv 
2013-03-29 12:36:19.265 PDT,"postgres","postgres",2324,"[local]",5155d681.914,10,"SELECT",2013-03-29 10:59:29 PDT,2/0,0,LOG,00000,"duration: 4027.183 ms  statement: select now(),pg_sleep(4);",,,,,,,,,"psql"
可以看到只记录了4秒的那个SQL,而没有记录1秒的SQL。 
原创粉丝点击