如何应用执行的SQL的执行计划

通过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)

  

 

如何应用执行的SQL的执行计划

上一篇:MYSQL排序


下一篇:OLE DB provider "SQLNCLI10" for linked server "x.x.x.x" returned message "No transaction is active.".