通過explain ,我們可以獲取特定SQL 的執行計划。但對於同一條SQL,不同的變量、不同的系統負荷,其執行計划可能不同。我們要如何取得SQL執行時間點的執行計划?KingbaseES 提供了 auto_explain 擴展插件,可以自動跟蹤SQL執行計划。
1、設置參數
設置參數:
shared_preload_libraries = 'auto_explain'
auto_explain.log_min_duration = 1000
auto_explain.log_min_duration 是最短語句執行時間(以毫秒為單位),將此設置為0 將記錄所有計划。-1(默認)禁用計划記錄。
2、查看SQL 執行計划
2021-08-30 17:35:06.797 CST [113562] LOG: duration: 0.010 ms plan: Query Text: select * from t1,t2 where t1.id1=t2.id2; Merge Join (cost=166.75..280.75 rows=7200 width=80) Merge Cond: (t1.id1 = t2.id2) -> Sort (cost=83.37..86.37 rows=1200 width=40) Sort Key: t1.id1 -> Seq Scan on t1 (cost=0.00..22.00 rows=1200 width=40) -> Sort (cost=83.37..86.37 rows=1200 width=40) Sort Key: t2.id2 -> Seq Scan on t2 (cost=0.00..22.00 rows=1200 width=40)
3、explain analyze
設置參數 auto_explain.log_analyze=on , 相當於 explain analyze :
2021-08-30 19:55:09.506 CST [121850] LOG: duration: 0.041 ms plan: Query Text: select * from t1,t2 where t1.id1=t2.id2; Merge Join (cost=166.75..280.75 rows=7200 width=80) (actual time=0.037..0.038 rows=0 loops=1) Merge Cond: (t1.id1 = t2.id2) -> Sort (cost=83.37..86.37 rows=1200 width=40) (actual time=0.035..0.036 rows=0 loops=1) Sort Key: t1.id1 Sort Method: quicksort Memory: 25kB -> Seq Scan on t1 (cost=0.00..22.00 rows=1200 width=40) (actual time=0.002..0.002 rows=0 loops=1) -> Sort (cost=83.37..86.37 rows=1200 width=40) (never executed) Sort Key: t2.id2 -> Seq Scan on t2 (cost=0.00..22.00 rows=1200 width=40) (never executed)