其实也不完全算PostgreSQL的技巧,应该算是SHELL技巧。

首先要配置好postgresql.conf,让PG记录下慢查询,并且日志固定格式,如下:
log_destination = 'csvlog'
log_min_duration_statement = 100ms
pg_ctl reload -D $PGDATA
 
来看一个统计的例子:
vi digoal.sh
 
#!/bin/bash
if [ $# -ne 2 ]; then
echo "Use 2 parameter"
exit
fi
 
file=$1
slow=$2
cnt=0
for i in `grep duration $1|grep SELECT|awk '{print $6}'|awk -F "." '{print $1}'`
do
if [ $i -gt $slow ]; then
cnt=$(($cnt+1))
fi
done
echo "Count Slow Sql (>$slow ms) In The $file : $cnt"
 
chmod 500 digoal.sh
 
例如,查询7月14号的慢查询,大于500MS的有多少条:
./digoal.sh "/var/log/pg_log/postgresql-2011-07-14*.csv" 500
Count Slow Sql (>500 ms) In The /var/log/pg_log/postgresql-2011-07-14*.csv : 13324
一天13324条执行时间超过500MS的SQL。
 
转载自:
https://blog.csdn.net/wan_xie2009/article/details/7621027
 posted on 2019-07-09 11:21  xibuhaohao  阅读(621)  评论(0编辑  收藏  举报