1. 程式人生 > 其它 >KingbaseES效能調優《索引優化建議》

KingbaseES效能調優《索引優化建議》

本文主要介紹KingbaseES使用外掛(索引優化建議)進行資料庫效能調優。

一、外掛 sys_qualstats

1、簡介
sys_qualstats 是一個 KingbaseES 的擴充套件,用於儲存 WHERE 語句和 JOIN 子句中謂詞的統計資訊。
如果希望能夠分析資料庫中最常執行的 quals(謂詞),這非常的有用,該外掛利用這點來提供索引建議。

2、外掛 sys_qualstats 載入方式
在使用 sys_qualstats 之前,我們需要將他新增到 kingbase.conf 檔案的 shared_preload_libraries 中,並重啟KingbaseES 資料庫。

shared_preload_libraries = 'sys_qualstats,sys_stat_statements' # (change requires restart)

3、建立外掛

create extension sys_qualstats;
create extension sys_stat_statements;

系統自動建立 sys_stat_statements 外掛。

二、使用sys_qualstats外掛

1、修改資料庫引數配置
kingbase.conf 中設定

shared_preload_libraries = 'sys_qualstats,sys_stat_statements,sys_hypo' # (change requires restart)
sys_stat_statements.track='top'
sys_qualstats.sample_rate=1

2、建立外掛

create extension sys_qualstats;
create extension sys_hypo;
create extension sys_stat_statements; --外掛 sys_stat_statements 由系統自動建立

三、測試案例

1、準備測試資料

create table t1(id int, name text);
INSERT INTO t1 SELECT generate_series(1, 1000000), md5(generate_series(1, 1000000)::text);

2、查詢SQL

select * from t1 where
id = 100; id | name -----+---------------------------------- 100 | f899139df5e1059396431415e770c6dd (1 行記錄)

3、檢視執行計劃

explain analyze Select * from t1 where id = 100;
QUERY PLAN
------------------------------------------------------------------------------------------------------------------
Gather (cost=1000.00..14612.43 rows=1 width=37) (actual time=0.199..45.577 rows=1 loops=1)
Workers Planned: 2
Workers Launched: 2
-> Parallel Seq Scan on t1 (cost=0.00..13612.33 rows=1 width=37) (actual time=11.960..24.969 rows=0 loops=3)
Filter: (id = 100)
Rows Removed by Filter: 333333
Planning Time: 0.041 ms
Execution Time: 45.611 ms
(8 行記錄)

4、檢視索引建議
new_cost: 建立索引以後的 cost 值
old_cost 無索引情況的 cost 值
ddl_index 建立 index 的 sql 語句
benefit 建立索引後的收益值

test=# \x
擴充套件顯示已開啟.
test=# select * from index_recommendation_by_qual;
-[ RECORD 1 ]---+-------------------------------------------
nspname | public
relid | t1
attnames | {id}
possible_types | {bitmap,brin,btree,hash}
execution_count | 1000000
queryid | 6060413669021853650
query | select * from t1 where id = 100;
ddl_index | CREATE INDEX ON public.t1 USING btree (id)
old_cost | 14612.43
new_cost | 8.07
benefit | 99.94 %

5、根據索引建議建立索引

CREATE INDEX ON public.t1 USING btree (id);

6、檢視建立完索引後的執行計劃

test=# explain analyze Select * from t1 where id = 100;
                                                  QUERY PLAN
---------------------------------------------------------------------------------------------------------------
 Index Scan using t1_id_idx on t1  (cost=0.42..8.44 rows=1 width=37) (actual time=0.021..0.022 rows=1 loops=1)
   Index Cond: (id = 100)
 Planning Time: 0.068 ms
 Execution Time: 0.049 ms
(4 行記錄)

7、再次檢視索引建議

test=# select * from index_recommendation_by_qual;
nspname | relid | attnames | possible_types | execution_count | queryid | query | ddl_index | old_cost | new_cost | benefit
---------+-------+----------+----------------+-----------------+---------+-------+-----------+----------+----------+---------
(0 行記錄)