Append

名称

结果合并算子

介绍

Append算子对两个或多个输入语句进行处理,先返回第一个输入语句的结果,然后返回下个输入的结果,以此类推,直至返回所有结果,可与其他算子配合来实现UNION (ALL)操作。在实现UNION操作中,相比于 MergeAppend 算子,Append算子对先合并子计划的结果,然后再进行 Sort (排序)和 Unique (去重)。

注解

UNION操作用于合并两个或多个 SELECT 语句的结果集并对合并后的结果进行排序和去重处理。若允许结果出现重复值,则用UNION ALL操作。

举例

--清理环境
DROP TABLE t1 CASCADE;

--创建环境
CREATE TABLE t1(a int primary key, b int);
INSERT INTO t1 VALUES(1,12);
INSERT INTO t1 VALUES(2,13);
INSERT INTO t1 VALUES(3,14);
INSERT INTO t1 VALUES(4,14);
ANALYZE t1;

EXPLAIN ANALYZE SELECT * FROM t1 WHERE a<2 UNION SELECT * FROM t1 WHERE b=14;
                                                                      QUERY PLAN
------------------------------------------------------------------------------------------------------------------------------------------------------
 Unique  (cost=11.22..11.25 rows=1 width=8) (actual time=0.06..0.07 rows=3 loops=1)
   ->  Sort  (cost=11.22..11.23 rows=3 width=8) (actual time=0.06..0.06 rows=3 loops=1)
         Sort Key: A, B
         ->  Append  (cost=0.01..11.19 rows=3 width=8) (actual time=0.03..0.05 rows=3 loops=1)
               ->  Subquery Scan "*SELECT* 1"  (cost=0.01..4.14 rows=1 width=8) (actual time=0.03..0.03 rows=1 loops=1)
                     ->  Index Scan using T1_PKEY(Normal Index Scan) on T1  (cost=0.01..4.14 rows=1 width=8) (actual time=0.02..0.02 rows=1 loops=1)
                           Index Key: (A < 2)
               ->  Subquery Scan "*SELECT* 2"  (cost=0.00..7.05 rows=2 width=4) (actual time=0.02..0.02 rows=2 loops=1)
                     ->  Seq Scan on T1  (cost=0.00..7.05 rows=2 width=4) (actual time=0.02..0.02 rows=2 loops=1)
                           Scan Key: (B = 14)
 Planning Time: 1.35 msec
 Execution Time: 0.41 msec
(12 rows)