更新时间:2025-05-29 GMT+08:00

SubQuery Scan

算子说明

当执行一个包含子查询的语句时,如果优化器RBO没有对它进行优化,它会先执行子查询的查询计划树,然后将子查询的结果传递给上层查询。

典型场景

当语句中包含子查询的时候,会生成SubQuery Scan算子从子查询中获取元组。

示例

--数据准备。 
gaussdb=# CREATE TABLE t_distinct(a int, b int, c int, d int, e regproc);

--执行结果。 
gaussdb=# EXPLAIN (costs off, nodes off) SELECT COUNT(DISTINCT(a)), COUNT(DISTINCT(b)), d FROM t_distinct GROUP BY c, d;
                                                    QUERY PLAN
-------------------------------------------------------------------------------------------------------------------
 Streaming (type: GATHER)
   ->  Hash Join
         Hash Cond: ((public.t_distinct.c = subquery."?column?") AND (public.t_distinct.d = subquery.d))
         ->  GroupAggregate
               Group By Key: public.t_distinct.c, public.t_distinct.d
               ->  Sort
                     Sort Key: public.t_distinct.c, public.t_distinct.d
                     ->  Streaming(type: REDISTRIBUTE)
                           ->  Seq Scan on t_distinct
         ->  Hash
               ->  Subquery Scan on subquery
                     ->  HashAggregate
                           Group By Key: public.t_distinct.c, public.t_distinct.d
                           ->  Streaming(type: REDISTRIBUTE)
                                 ->  HashAggregate
                                       Group By Key: public.t_distinct.c, public.t_distinct.d, public.t_distinct.a
                                       ->  Seq Scan on t_distinct
(17 rows)

--删除表。
gaussdb=# DROP TABLE t_distinct;

上述示例中,Subquery Scan算子输出信息如下所示。

信息名称

含义

Subquery Scan

算子的名称。

HashAggregate

算子的名称。