We are using version 2.20
Here’s the plan with hash join:
Hash Anti Join (cost=726.88..830.66 rows=1 width=131) (actual time=453.432..492.201 rows=10980 loops=1)
Hash Cond: (((sg.shard_id)::text = (vc.shard_id)::text) AND ((sg.sgid)::text = (vc.sgid)::text))
Buffers: temp read=19405 written=19405
-> Hash Join (cost=611.88..715.64 rows=1 width=163) (actual time=422.667..441.957 rows=101880 loops=1)
Hash Cond: ((vst.stid)::text = (sg.stid)::text)
Buffers: temp read=18009 written=18009
-> Seq Scan on tablest vst (cost=0.00..100.00 rows=1000 width=32) (actual time=0.318..0.524 rows=14 loops=1)
Filter: ((flagd IS FALSE) AND (flagp IS TRUE))
Storage Table Read Requests: 4
Storage Table Read Execution Time: 0.448 ms
Storage Table Rows Scanned: 14
-> Hash (cost=611.87..611.87 rows=1 width=195) (actual time=422.323..422.323 rows=101880 loops=1)
Buckets: 32768 (originally 1024) Batches: 32 (originally 1) Memory Usage: 3841kB
Buffers: temp read=15150 written=17082
-> Hash Join (cost=508.11..611.87 rows=1 width=195) (actual time=379.788..404.767 rows=101880 loops=1)
Hash Cond: ((sa.shard_id)::text = (sg.shard_id)::text)
Buffers: temp read=15150 written=15150
-> Seq Scan on tablesa sa (cost=0.00..100.00 rows=1000 width=32) (actual time=0.538..3.268 rows=15210 loops=1)
Filter: ((flagd IS FALSE) AND (flagp IS TRUE))
Storage Table Read Requests: 16
Storage Table Read Execution Time: 1.970 ms
Storage Table Rows Scanned: 15210
-> Hash (cost=508.10..508.10 rows=1 width=259) (actual time=379.188..379.188 rows=101880 loops=1)
Buckets: 16384 (originally 1024) Batches: 8 (originally 1) Memory Usage: 3969kB
Buffers: temp read=11542 written=14207
-> Hash Join (cost=398.09..508.10 rows=1 width=259) (actual time=318.096..359.595 rows=101880 loops=1)
Hash Cond: (((g.shard_id)::text = (sg.shard_id)::text) AND ((g.grp_id)::text = (sg.grp_id)::text))
Buffers: temp read=11542 written=11542
-> Seq Scan on tableg g (cost=0.00..102.50 rows=1000 width=97) (actual time=0.822..9.929 rows=69570 loops=1)
Remote Filter: ((grp_status)::text = 'active'::text)
Filter: ((flagd IS FALSE) AND (flagp IS TRUE))
Storage Table Read Requests: 18
Storage Table Read Execution Time: 1.725 ms
Storage Table Rows Scanned: 69570
-> Hash (cost=398.07..398.07 rows=1 width=194) (actual time=317.188..317.188 rows=102240 loops=1)
Buckets: 32768 (originally 1024) Batches: 8 (originally 1) Memory Usage: 3841kB
Buffers: temp read=7941 written=9921
-> Hash Join (cost=285.56..398.07 rows=1 width=194) (actual time=265.672..300.243 rows=102240 loops=1)
Hash Cond: (((c.pid)::text = (sg.pid)::text) AND ((c.shard_id)::text = (sg.shard_id)::text))
Buffers: temp read=7941 written=7941
-> Seq Scan on tablep c (cost=0.00..102.50 rows=1000 width=66) (actual time=2.879..8.124 rows=44190 loops=1)
Remote Filter: ((pstatus)::text = 'active'::text)
Filter: ((flagd IS FALSE) AND (flagp IS TRUE))
Storage Table Read Requests: 12
Storage Table Read Execution Time: 3.455 ms
Storage Table Rows Scanned: 46080
-> Hash (cost=285.19..285.19 rows=25 width=192) (actual time=262.675..262.675 rows=137700 loops=1)
Buckets: 32768 (originally 1024) Batches: 8 (originally 1) Memory Usage: 3841kB
Buffers: temp read=4061 written=6749
-> Hash Join (cost=115.00..285.19 rows=25 width=192) (actual time=89.609..235.752 rows=137700 loops=1)
Hash Cond: (((cv.shard_id)::text = (sg.shard_id)::text) AND ((cv.sgid)::text = (sg.sgid)::text))
Buffers: temp read=4061 written=4061
-> Seq Scan on tablec cv (cost=0.00..100.00 rows=1000 width=64) (actual time=1.978..80.364 rows=140220 loops=1)
Storage Table Read Requests: 140
Storage Table Read Execution Time: 53.664 ms
Storage Table Rows Scanned: 140220
-> Hash (cost=100.00..100.00 rows=1000 width=160) (actual time=87.490..87.490 rows=128160 loops=1)
Buckets: 32768 (originally 1024) Batches: 8 (originally 1) Memory Usage: 3841kB
Buffers: temp written=2098
-> Seq Scan on tablesg sg (cost=0.00..100.00 rows=1000 width=160) (actual time=1.390..54.860 rows=128160 loops=1)
Filter: ((flagd IS FALSE) AND (flagp IS TRUE))
Storage Table Read Requests: 128
Storage Table Read Execution Time: 27.325 ms
Storage Table Rows Scanned: 128160
-> Hash (cost=100.00..100.00 rows=1000 width=64) (actual time=30.728..30.728 rows=65430 loops=1)
Buckets: 65536 (originally 1024) Batches: 2 (originally 1) Memory Usage: 3585kB
Buffers: temp written=311
-> Seq Scan on tablecl vc (cost=0.00..100.00 rows=1000 width=64) (actual time=0.625..22.835 rows=65430 loops=1)
Storage Table Read Requests: 66
Storage Table Read Execution Time: 17.840 ms
Storage Table Rows Scanned: 65430
Planning Time: 10.015 ms
Execution Time: 492.722 ms
Storage Read Requests: 384
Storage Read Execution Time: 106.427 ms
Storage Rows Scanned: 464684
Storage Write Requests: 0
Catalog Read Requests: 0
Catalog Write Requests: 0
Storage Flush Requests: 0
Storage Execution Time: 106.427 ms
Peak Memory Usage: 25975 kB
The DDL are:
CREATE TABLE IF NOT EXISTS public.tablesa (
shard_id varchar NOT NULL,
shard_name varchar NULL,
mid varchar NOT NULL,
il varchar NULL,
zo INTERVAL NULL,
aab numeric NULL,
cl numeric NULL,
ccode varchar NULL,
ra numeric NULL,
created_time timestamptz NULL DEFAULT now(),
created_by varchar NULL,
flagp bool NOT NULL DEFAULT 'false',
tid varchar NULL,
last_user_update_time timestamptz NULL DEFAULT now(),
last_user_update_by varchar NULL,
flagd bool NOT NULL DEFAULT 'false',
CONSTRAINT tablesa_pk PRIMARY KEY (shard_id)
);
CREATE TABLE IF NOT EXISTS public.tablep (
pid varchar NOT NULL,
shard_id varchar NOT NULL,
pname varchar NOT NULL,
ptype varchar NOT NULL,
psubtype varchar NULL,
pstatus varchar NOT NULL DEFAULT 'pending',
duration_type varchar NULL,
lt int2 NOT NULL DEFAULT 16,
start_date timestamptz NULL,
end_date timestamptz NULL,
aci int8 NOT NULL DEFAULT 0,
tci int8 NOT NULL DEFAULT 0,
created_time timestamptz NULL DEFAULT now(),
created_by varchar NULL,
flagp bool NOT NULL DEFAULT 'false',
last_user_update_time timestamptz NULL DEFAULT now(),
last_user_update_by varchar NULL,
flagd bool NOT NULL DEFAULT 'false',
CONSTRAINT tablep_pk PRIMARY KEY (shard_id, pid)
);
CREATE INDEX IF NOT EXISTS idx_tablep_pname ON public.tablep (pname, flagd);
CREATE TABLE IF NOT EXISTS public.tableg (
grp_id varchar NOT NULL,
grp_name varchar NOT NULL,
grp_type varchar NULL DEFAULT 'Standard'::character varying,
grp_status varchar NOT NULL DEFAULT 'pending',
pid varchar NOT NULL,
shard_id varchar NOT NULL,
flagf bool NOT NULL DEFAULT 'true',
created_time timestamptz NULL DEFAULT now(),
created_by varchar NULL,
flagp bool NOT NULL DEFAULT 'false',
last_user_update_time timestamptz NULL DEFAULT now(),
last_user_update_by varchar NULL,
flagd bool NOT NULL DEFAULT 'false',
CONSTRAINT tableg_pk PRIMARY KEY (shard_id, grp_id)
);
CREATE INDEX IF NOT EXISTS idx_tableg_grp_name ON public.tableg (grp_name, flagd);
CREATE TABLE IF NOT EXISTS public.tablesg (
sgid varchar NOT NULL,
sgname varchar NOT NULL,
grp_id varchar NOT NULL,
pid varchar NOT NULL,
shard_id varchar NOT NULL,
tsgi int8 NOT NULL DEFAULT 0,
af_id varchar,
stid varchar,
created_time timestamptz NULL DEFAULT now(),
created_by varchar NULL,
flagp bool NOT NULL DEFAULT 'false',
last_user_update_time timestamptz NULL DEFAULT now(),
last_user_update_by varchar NULL,
flagd bool NOT NULL DEFAULT 'false',
tsgb numeric NOT NULL DEFAULT 0,
asgs numeric NOT NULL DEFAULT 0,
atype varchar NULL,
asgi int8 NOT NULL DEFAULT 0,
CONSTRAINT tablesg_pk PRIMARY KEY (shard_id, sgid)
);
CREATE INDEX IF NOT EXISTS idx_tablesg_grp_f ON public.tablesg (grp_id, af_id, stid, flagd);
CREATE TABLE IF NOT EXISTS public.tablec (
cid varchar NOT NULL,
cname varchar NOT NULL,
sgid varchar NOT NULL,
grp_id varchar NOT NULL,
pid varchar NOT NULL,
shard_id varchar NOT NULL,
cstatus varchar NOT NULL DEFAULT 'pending',
cdescription varchar NULL,
created_time timestamptz NULL DEFAULT now(),
created_by varchar NULL,
flagp bool NOT NULL DEFAULT 'false',
last_user_update_time timestamptz NULL DEFAULT now(),
last_user_update_by varchar NULL,
flagd bool NOT NULL DEFAULT 'false',
ctype varchar NULL,
CONSTRAINT tablec_pk PRIMARY KEY (shard_id, cid)
);
CREATE INDEX IF NOT EXISTS idx_tablec_cname ON public.tablec (cname, flagd);
CREATE TABLE IF NOT EXISTS public.tableti (
grp_id varchar NOT NULL,
pid varchar NOT NULL,
shard_id varchar NOT NULL,
day_of_week int2 NOT NULL,
t_start_time varchar NOT NULL,
t_end_time varchar NOT NULL,
ntstart_time timestamptz NULL,
ntend_time timestamptz NULL,
created_time timestamptz NULL DEFAULT now(),
created_by varchar NULL,
flagp bool NOT NULL DEFAULT 'false',
last_user_update_time timestamptz NULL DEFAULT now(),
last_user_update_by varchar NULL,
flagd bool NOT NULL DEFAULT 'false',
CONSTRAINT tableti_pk PRIMARY KEY (shard_id, grp_id, day_of_week, t_start_time)
);
CREATE TABLE IF NOT EXISTS public.tablest (
stid varchar NOT NULL,
stname varchar NOT NULL,
stdesc varchar NULL,
created_time timestamptz NULL DEFAULT now(),
created_by varchar NULL,
flagp bool NOT NULL DEFAULT 'false',
last_user_update_time timestamptz NULL DEFAULT now(),
last_user_update_by varchar NULL,
flagd bool NOT NULL DEFAULT 'false',
CONSTRAINT tablest_pk PRIMARY KEY (stid)
);
CREATE INDEX IF NOT EXISTS idx_tablest_st_name ON public.tablest (stname, flagd);
CREATE TABLE IF NOT EXISTS public.tablesgh (
sgid varchar NOT NULL,
st_start_time timestamptz NOT NULL,
st_end_time timestamptz NOT NULL,
tsgi int8 NOT NULL DEFAULT 0,
asgi int4 NOT NULL DEFAULT 0,
shard_id varchar NOT NULL DEFAULT '',
CONSTRAINT tablesgh_pk PRIMARY KEY (shard_id, sgid, st_start_time)
);
CREATE TABLE IF NOT EXISTS public.tablecl (
sgid varchar NOT NULL,
grp_id varchar NOT NULL,
pid varchar NOT NULL,
created_time timestamptz NOT NULL DEFAULT now(),
shard_id varchar NOT NULL DEFAULT '',
lt int2 NULL,
flagf bool NULL,
CONSTRAINT tablecl_pk PRIMARY KEY (shard_id, sgid)
);
CREATE INDEX IF NOT EXISTS idx_tablep_updtime ON public.tablep (last_user_update_time);
CREATE INDEX IF NOT EXISTS idx_tableg_updtime ON public.tableg (last_user_update_time);
CREATE INDEX IF NOT EXISTS idx_tablesg_updtime ON public.tablesg (last_user_update_time);
CREATE INDEX IF NOT EXISTS idx_tablec_updtime ON public.tablec (last_user_update_time);
CREATE INDEX IF NOT EXISTS idx_tablest_updtime ON public.tablest (last_user_update_time);
Appreciate your help FranckPachot.