更新时间:2025-05-29 GMT+08:00
InitPlan和Subplan
算子说明
InitPlan是GaussDB的子计划的一部分。GaussDB中子查询计划可分为相关子计划和非相关子计划,相关子计划是指子查询依赖外部查询的行,不可独立于外部查询执行,非相关子计划则相反。在GaussDB中,SubPlan或InitPlan都可以叫做子计划,是相对于整个计划而言可以相对独立执行的部分,一般由不能提升的子计划生成。SubPlan主要是相关子计划生成的,InitPlan则是非相关子计划生成的。SubPlan是在主查询执行期间运行的,在主查询的每一行上重新执行一次,而InitPlan是在主查询执行之前运行的,结果是一次性的,它们在查询开始时计算一次,然后缓存起来在整个查询执行期间重用,所以InitPlan效率会更高。
典型场景
SELECT子计划中不依赖外部查询,又不能进行提升。
示例
示例:SELECT子计划中不依赖外部查询,又不能进行提升。
--数据准备。 gaussdb=# DROP TABLE IF EXISTS init_table; gaussdb=# DROP TABLE IF EXISTS t1; gaussdb=# DROP TABLE IF EXISTS t2; gaussdb=# CREATE TABLE init_table (id int, grade int, time int); CREATE TABLE gaussdb=# INSERT INTO init_table VALUES(generate_series(1,10000), (random() * 10)::integer,(random() * 10)::integer ); INSERT 0 10000 gaussdb=# CREATE TABLE t1(grade int); CREATE TABLE gaussdb=# INSERT INTO t1 VALUES( 10),( 11); INSERT 0 2 gaussdb=# CREATE TABLE t2(a int, b int, c int); CREATE TABLE gaussdb=# INSERT INTO t2 VALUES(1, 2, 3 ); INSERT 0 1 --执行结果。 gaussdb=# EXPLAIN SELECT * FROM init_table WHERE init_table.grade IN (SELECT grade FROM t2) AND init_table.grade != (SELECT * FROM t1); QUERY PLAN ------------------------------------------------------------------------------------------ Streaming (type: GATHER) (cost=13.56..126.53 rows=9 width=12) Node/s: All datanodes InitPlan 2 (returns $2) -> Streaming(type: BROADCAST) (cost=0.00..13.18 rows=40 width=4) Spawn on: All datanodes -> Seq Scan on t1 (cost=0.00..13.13 rows=20 width=4) -> Seq Scan on init_table (cost=0.00..112.79 rows=9 width=12) Filter: ((grade <> $2) AND (SubPlan 1)) SubPlan 1 -> Result (cost=0.00..15.28 rows=40 width=0) -> Materialize (cost=0.00..15.28 rows=40 width=0) -> Streaming(type: BROADCAST) (cost=0.00..15.18 rows=40 width=0) Spawn on: All datanodes -> Seq Scan on t2 (cost=0.00..13.13 rows=20 width=0) (14 rows) --删除。 gaussdb=# DROP TABLE IF EXISTS init_table; gaussdb=# DROP TABLE IF EXISTS t1; gaussdb=# DROP TABLE IF EXISTS t2;
上述示例中,输出信息如下所示。
信息名称 |
含义 |
---|---|
InitPlan |
非相关子计划。示例中InitPlan 2表示,子计划2为非相关子计划。 |
returns |
返回的结果。示例中returns $2为将InitPlan的结果存在$2中。 |
SubPlan |
相关子计划。示例中SubPlan 1表示,子计划1为相关子计划。 |
父主题: 其他关键字