prepare_custom_count¶
用在prepare语句中。决定在prepare定义查询计划后、通过execute使用该查询计划时,控制使用通用计划或定制计划。
注解
通用计划: 在prepare时会产生一个计划称为通用计划,之后在execute时无论参数是什么,都不再重新生成计划,直接使用prepare时生成的计划。
定制计划: execute时,根据当时传入的参数,重新生成计划,而非使用通用计划。
参数¶
custom_count
custom_count是int类型整数,范围是0~2147483647。超过最大值会被截断为最大值。
注解
- custom_count = 0:
- 不生成定制计划,即 execute 时不会再生成计划
- custom_count > 0:
前custom_count次 execute 一定会生成定制计划,并使用定制计划
custom_count次以后,根据前n次的计划估算是否存在数据倾斜处理
- 如果存在后续的所有 execute 一定都会再生成定制计划并使用定制计划
- 如果不存在数据倾斜则后续所有 execute 不再生成计划,恒使用通用计划
示例¶
-- 清理环境
drop table a;
-- 建表
create table a(a int,b int);
insert into a values(1,1);
insert into a select * from a;
insert into a select * from a;
insert into a select * from a;
insert into a select * from a;
insert into a select * from a;
insert into a select * from a;
insert into a select * from a;
insert into a select * from a;
insert into a select * from a;
insert into a select * from a;
insert into a values(2,2);
create index idx on a(a);
analyze a;
--使用hint,参数为0,不生成自定义计划
prepare p1 as select /*+ PREPARE_CUSTOM_COUNT(0) */ * from a where a=?;
explain execute p1(1);
QUERY PLAN(text)
----------------------
Index Scan using IDX(Normal Index Scan) on A (cost=0.03..17.21 rows=513 width=8)
Index Key: (A = $1)
总数目:2
explain execute p1(2);
QUERY PLAN(text)
----------------------
Index Scan using IDX(Normal Index Scan) on A (cost=0.03..17.21 rows=513 width=8)
Index Key: (A = $1)
总数目:2
--不使用hint,对比计划
prepare p11 as select * from a where a=?;
explain execute p11(1);
QUERY PLAN(text)
----------------------
Seq Scan on A (cost=0.00..20.81 rows=1024 width=8)
Scan Key: (A = 1)
总数目:2
explain execute p11(2);
QUERY PLAN(text)
----------------------
Index Scan using IDX(Normal Index Scan) on A (cost=0.03..1.05 rows=1 width=8)
Index Key: (A = 2)
总数目:2
drop table a cascade;