postgresql parallel join example

CREATE TABLE public.pgbench_accounts_bak (
aid integer NOT NULL,
bid integer,
abalance integer,
filler character(84)
);

ALTER TABLE public.pgbench_accounts_bak OWNER TO postgres;


ALTER TABLE ONLY public.pgbench_accounts_bak
ADD CONSTRAINT pgbench_accounts_bak_pkey PRIMARY KEY (aid);

explain select count(*) from pgbench_accounts;
Finalize Aggregate (cost=22188.97..22188.98 rows=1 width=8)
-> Gather (cost=22188.76..22188.97 rows=2 width=8)
Workers Planned: 2
-> Partial Aggregate (cost=21188.76..21188.77 rows=1 width=8)
-> Parallel Index Only Scan using pgbench_accounts_pkey on pgbench_accounts (cost=0.42..20147.09 rows=416667 width=0)


explain select * from pgbench_accounts a join pgbench_branches b on a.bid=b.bid ;
youge=# set parallel_setup_cost=1;
SET
youge=# set parallel_tuple_cost=0.001;
SET
youge=# explain select * from pgbench_accounts a join pgbench_branches_bak b on a.bid=b.bid ;
QUERY PLAN
-----------------------------------------------------------------------------------------------------------
Gather (cost=13744.46..57865.89 rows=1000000 width=461)
Workers Planned: 2
-> Parallel Hash Join (cost=13743.46..56864.89 rows=416667 width=461)
Hash Cond: (a.bid = b.bid)
-> Parallel Seq Scan on pgbench_accounts a (cost=0.00..20560.67 rows=416667 width=97)
-> Parallel Hash (cost=3092.65..3092.65 rows=176465 width=364)
-> Parallel Seq Scan on pgbench_branches_bak b (cost=0.00..3092.65 rows=176465 width=364)
(7 rows)

explain select * from pgbench_accounts a1 join pgbench_accounts_bak a2 on a1.bid = a2.bid where a2.aid >1000;
QUERY PLAN
------------------------------------------------------------------------------------------------
Merge Join (cost=473145.77..1499198922.65 rows=99914552113 width=194)
Merge Cond: (a2.bid = a1.bid)
-> Sort (cost=237718.93..240216.52 rows=999037 width=97)
Sort Key: a2.bid
-> Seq Scan on pgbench_accounts_bak a2 (cost=0.00..28894.00 rows=999037 width=97)
Filter: (aid > 1000)
-> Materialize (cost=235426.84..240426.84 rows=1000000 width=97)
-> Sort (cost=235426.84..237926.84 rows=1000000 width=97)
Sort Key: a1.bid
-> Seq Scan on pgbench_accounts a1 (cost=0.00..26394.00 rows=1000000 width=97)
(10 rows)


create index pgbench_accounts_bid_key on pgbench_accounts_bak(bid);

youge=# set parallel_setup_cost=1;
SET
youge=# set parallel_tuple_cost=0.001;
SET
youge=# explain select * from pgbench_accounts_bak a join pgbench_branches_bak b on a.bid=b.bid ;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------
Gather (cost=26251.42..42754.18 rows=1000000 width=461)
Workers Planned: 2
-> Merge Join (cost=26250.42..41753.18 rows=416667 width=461)
Merge Cond: (a.bid = b.bid)
-> Parallel Index Scan using pgbench_accounts_bid_key on pgbench_accounts_bak a (cost=0.42..36544.09 rows=416667 width=97)
-> Index Scan using pgbench_branches_bak_pkey on pgbench_branches_bak b (cost=0.42..9131.27 rows=299990 width=364)
(6 rows)

原文地址:https://www.cnblogs.com/youge-OneSQL/p/9913550.html