ParallelStream

名称

并行流算子

介绍

ParallelStream算子实现了在分区表的不同分区或者不同子计划上的并行操作,通过上层的 SerialStream 算子将并行的所有结果集进行记录

神通数据库默认下关闭了并行操作,使用时需要对数据库中ENABLE_PARALLEL、PARALLEL_DEGREE、MIN_PARALLEL_DEGREE等相关参数进行设置

举例

--清理环境
DROP TABLE t1 CASCADE;
DROP TABLE t2 CASCADE;

--分区表情况下的并行
CREATE TABLE t1(a int) PARTITION BY HASH(a) PARTITIONS 4;

EXPLAIN ANALYZE SELECT * FROM t1;
                                                             QUERY PLAN
--------------------------------------------------------------------------------------------------------------------
 SerialStream ( 2 Parallel Streams )  (cost=1.00..44.96 rows=4096 width=4) (actual time=0.01..0.01 rows=0 loops=1)
   ->  ParallelStream  (cost=1.00..22.48 rows=2048 width=4) (actual time=0.00..0.00 rows=0 loops=1)
         ->  Partition Seq Scan on T1(1,3)  (cost=1.00..22.48 rows=2048 width=4) (never executed)
 Planning Time: 0.09 msec
 Execution Time: 2.50 msec
(5 rows)

--不用子计划的并行
CREATE TABLE t2(a int primary key, b int);
INSERT INTO t2 VALUES(1,12);
INSERT INTO t2 VALUES(2,13);
INSERT INTO t2 VALUES(3,14);
INSERT INTO t2 VALUES(4,14);

EXPLAIN ANALYZE SELECT * FROM t2 WHERE a<2 UNION SELECT * FROM t2 WHERE b=14;
                                                               QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------
 Unique  (cost=305.20..325.99 rows=277 width=8) (actual time=0.03..0.03 rows=3 loops=1)
   ->  Sort  (cost=305.20..312.13 rows=2772 width=8) (actual time=0.03..0.03 rows=3 loops=1)
         Sort Key: A, B
         ->  SerialStream ( 2 Parallel Streams )  (cost=0.00..146.71 rows=2772 width=8) (actual time=0.00..0.00 rows=3 loops=1)
               ->  ParallelStream  (cost=0.00..36.31 rows=2731 width=8) (actual time=0.00..0.00 rows=1 loops=1)
                     ->  Subquery Scan "*SELECT* 1"  (cost=0.00..36.31 rows=2731 width=8) (never executed)
                           ->  Index Scan using T2_PKEY(Normal Index Scan) on T2  (cost=0.00..36.31 rows=2731 width=8) (never executed)
                                 Index Key: (A < 2)
 Planning Time: 3.56 msec
 Execution Time: 2.41 msec
(10 rows)

--清理环境
DROP TABLE t1 CASCADE;
DROP TABLE t2 CASCADE;