postgresql之性能優化


通過下面查詢可以計算緩存命中率,應該非常接近1,否則應該調整shared_buffers的配置,低於99%,可以嘗試調大它的值

postgres=# select blks_hit::float/(blks_read + blks_hit) as cache_hit_ratio from pg_stat_database where datname=current_database();
  cache_hit_ratio
-------------------
 0.997730115836991
(1 row)

查詢計算事務提交率,應該非常接近1,否則死鎖或其他超時太多

postgres=# select xact_commit::float/(xact_commit + xact_rollback) as successful_xact_ratio from pg_stat_database where datname = current_database();
 successful_xact_ratio
-----------------------
     0.986184210526316
(1 row)

 從性能角度看最有意義的數據是與索引vs順序掃描有關的統計信息,以下是計算索引掃描與整個數據庫的所有掃描的比率可以計算如下:

postgres=# select sum(idx_scan)/(sum(idx_scan) + sum(seq_scan)) as idx_scan_ratio from pg_stat_all_tables where schemaname ='public';
     idx_scan_ratio
------------------------
 0.03305785123966942149
postgres=# select relname,idx_scan::float/(idx_scan+seq_scan+1) as idx_scan_ratio from pg_stat_all_tables where schemaname='public' order by idx_scan_ratio ASC;
     relname     |  idx_scan_ratio
-----------------+-------------------
 test_json1      |                 0
 log_ins_201702  |                 0
 log_par201703   |                 0
 log_ins_201704  |                 0
 log_par201702   |                 0
 log_par201701   |                 0
 log_ins_201707  |                 0
 log_ins_201708  |                 0
 log_par_his     |                 0
 log_ins_201706  |                 0
 log_ins_201703  |                 0

關閉數據庫, 開啟pg_stat_statements

vi postgresql.conf

#shared_preload_libraries = ''          # (change requires restart)

shared_preload_libraries = 'pg_stat_statements'
pg_stat_statements.track = all

准備開啟數據庫報錯

 

 找到pg_stat_statements安裝目錄

/root/postgresql-10.0/contrib/pg_stat_statements 執行 make && make install

 

 之后重啟數據庫成功

安裝插件成功

 

 查看sql的執行計划

在分析INSERT,UPDATE,delete,create tbase as 或者execute的時候,應該開啟一個事務來執行,執行完之后進行回滾,避免因為用analyze對數據進行修改;

postgres=# begin ;
BEGIN
postgres=# create table ly(id int);
CREATE TABLE
postgres=# insert into ly values (1);
INSERT 0 1
postgres=# explain select * from ly ;
                      QUERY PLAN
------------------------------------------------------
 Seq Scan on ly  (cost=0.00..35.50 rows=2550 width=4)
(1 row)

postgres=# explain update ly set idid=2 wherr id=1;
id
postgres=# explain update ly set idid=2 wherr id=1;
id
postgres=# explain update ly set id=2 wherr id=1;
2021-10-19 06:22:49.383 EDT [3638] ERROR:  syntax error at or near "wherr" at character 28
2021-10-19 06:22:49.383 EDT [3638] STATEMENT:  explain update ly set id=2 wherr id=1;
ERROR:  syntax error at or near "wherr"
LINE 1: explain update ly set id=2 wherr id=1;
                                   ^
postgres=# explain update ly set id=2 where id=1;
2021-10-19 06:22:54.384 EDT [3638] ERROR:  current transaction is aborted, commands ignored until end of transaction block
2021-10-19 06:22:54.384 EDT [3638] STATEMENT:  explain update ly set id=2 where id=1;
ERROR:  current transaction is aborted, commands ignored until end of transaction block

以上報錯原因,begin沒有提交

這樣就可以了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM