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

SubQuery Scan

算子说明

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

典型场景

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

示例

示例:查询中带无法下推的子查询。

--数据准备。 
gaussdb=# CREATE TABLE t1(c1 number, c2 number, c3 number); 
CREATE TABLE 
gaussdb=# CREATE TABLE t2(c1 number, c2 number, c3 number); 
CREATE TABLE
gaussdb=# INSERT INTO t1 VALUES(generate_series(1,10000), (random() * 10)::integer,(random() * 10)::integer );
INSERT 0 10000
gaussdb=# INSERT INTO t2 VALUES(generate_series(1,10000), (random() * 10)::integer,(random() * 10)::integer );
INSERT 0 10000

--执行结果。 
gaussdb=# EXPLAIN SELECT * FROM t1 WHERE c2 > (SELECT avg(c2) FROM t2 WHERE t2.c1 = t1.c1);
                                  QUERY PLAN                                   
-------------------------------------------------------------------------------
 Hash Join  (cost=87.87..198.64 rows=886 width=96)
   Hash Cond: (t1.c1 = subquery."?column?")
   Join Filter: (t1.c2 > subquery.avg)
   ->  Seq Scan on t1  (cost=0.00..67.58 rows=2658 width=96)
   ->  Hash  (cost=85.37..85.37 rows=200 width=64)
         ->  Subquery Scan on subquery  (cost=80.87..85.37 rows=200 width=64)
               ->  HashAggregate  (cost=80.87..83.37 rows=200 width=64)
                     Group By Key: t2.c1
                     ->  Seq Scan on t2  (cost=0.00..67.58 rows=2658 width=64)
(9 rows)

--删除。
gaussdb=# DROP TABLE t1,t2;

信息名称

含义

SubQuery Scan

算子的名称。

Hash

内表创建hash table的算子。