KingbaseES中通过HINT修正行数估计
关键字:
优化器、HINT、行数估计、人大金仓、KingbaseES
一、摘要
本文介绍了KingbaseES中通过HINT修正行数估计的方法。
修正行数估计
是指在执行SQL的过程中,如果出现行数估计不准的情况,通过HINT修正行数估计,从而使得选择率计算准确,计划选择更精准,提高执行效率。
KingbaseES中使用自适应基数估计
首先,建立表t1,并插入数据,然后执行SQL语句。
create table t1(c1 int, c2 int, c3 int);
insert into t1 select i, i, i from generate_series(1,1000) as i;
analyze t1;
explain analyze select * from t1 where mod(c1, 3) = 0;
QUERY PLAN
------------------------------------------------------------------------------------------------
Seq Scan on t1 (cost=0.00..21.00 rows=5 width=12) (actual time=0.012..0.220 rows=333 loops=1)
Filter: (mod(c1, 3) = 0)
Rows Removed by Filter: 667
Planning Time: 0.037 ms
Execution Time: 0.246 ms
(5 rows)
可以看到行数估计是不准的,估计5行,而实际有333行。
接下来开启HINT,并再次执行添加HINT的SQL。
SET enable_hint TO on;
explain analyze select /*+Rows(t1 #333)*/ * from t1 where mod(c1, 3) = 0;
QUERY PLAN
--------------------------------------------------------------------------------------------------
Seq Scan on t1 (cost=0.00..21.00 rows=333 width=12) (actual time=0.016..0.227 rows=333 loops=1)
Filter: (mod(c1, 3) = 0)
Rows Removed by Filter: 667
Planning Time: 0.099 ms
Execution Time: 0.258 ms
(5 rows)
可以看到函数估计已经修正。




