1. 程式人生 > >pgsql使用積累系列_安裝pg_stat_statements獲取慢sql

pgsql使用積累系列_安裝pg_stat_statements獲取慢sql

 

1. 安裝postgresql(10.1)

參考連結:postgresql10安裝

2. 配置postgresql.conf檔案

開啟配置檔案找到shared_preload_libraries,新增pg_stat_statements,配置好要重啟pgsql。如下圖所示

3. 連線指定資料庫,並安裝pg_stat_statements外掛

psql -p 5433 -d pgtestdb -U admin

create extension pg_stat_statements

如下圖所示:

4. 檢視sql

重置:

select pg_stat_reset();
select pg_stat_statements_reset();

執行要分析的sql:

update rccl_passenger set is_guide = 1
from (select pax_id from rccl_group_leader) t
where rccl_passenger.guest_id = t.pax_id::int and rccl_passenger.synctime > current_date;

update rccl_passenger set is_guide = 1
from (select phone from rccl_external_gourp_leader) t
where rccl_passenger.phone = t.phone and rccl_passenger.synctime > current_date;

......

獲取執行最慢的top5 sql
select * from pg_stat_statements order by total_time desc limit 5;

獲取最耗資源的top5 sql

select * from pg_stat_statements order by shared_blks_hit+shared_blks_read desc limit 5;

分析慢sql:

PostgreSql的Explain命令詳解 toplchx這篇文章關於執行計劃分析比較詳細

EXPLAIN (ANALYZE,Buffers) update rccl_passenger set is_guide = 1
from (select pax_id from rccl_group_leader) t
where rccl_passenger.guest_id = t.pax_id::int and rccl_passenger.synctime > current_date

優化sql:

加索引,調記憶體配置等。