并行执行

最近更新时间: 2025-02-18 16:02:00

示例:parallel(n)指定SQL的并发数

  • 复制
    复制成功
postgres=# create table hint_t3(f1 integer,f2 integer) ;
CREATE TABLE
postgres=# insert into hint_t3 select t as f1,t as f2 from generate_series(1,1000000) as t;    
INSERT 0 1000000
postgres=# vacuum ANALYZE hint_t3;
VACUUM
postgres=# 
postgres=#  explain select count(1) from hint_t3;                                      
                                     QUERY PLAN                                      
-------------------------------------------------------------------------------------
 Finalize Aggregate  (cost=20020.01..20020.02 rows=1 width=8)
   ->  Remote Subquery Scan on all (dn001)  (cost=20020.00..20020.01 rows=1 width=0)
         ->  Partial Aggregate  (cost=19920.00..19920.01 rows=1 width=8)
               ->  Seq Scan on hint_t3  (cost=0.00..17420.00 rows=1000000 width=0)
(4 rows)

postgres=# explain select /*+parallel(hint_t3 2) */ count(1) from hint_t3;
                                           QUERY PLAN                                            
-------------------------------------------------------------------------------------------------
 Parallel Finalize Aggregate  (cost=13728.35..13728.36 rows=1 width=8)
   ->  Parallel Remote Subquery Scan on all (dn001)  (cost=13728.33..13728.35 rows=1 width=0)
         ->  Gather  (cost=13628.33..13628.34 rows=1 width=8)
               Workers Planned: 2
               ->  Partial Aggregate  (cost=12628.33..12628.34 rows=1 width=8)
                     ->  Parallel Seq Scan on hint_t3  (cost=0.00..11586.67 rows=416667 width=0)
(6 rows)
postgres=#