BEGIN; BEGIN EXPLAIN (ANALYZE, BUFFERS) select cntrycode, count(*) as numcust, sum(c_acctbal) as totacctbal from ( select substr(c_phone, 1, 2) as cntrycode, c_acctbal from customer where substr(c_phone, 1, 2) in ('24', '23', '32', '16', '21', '14', '17') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('24', '23', '32', '16', '21', '14', '17') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=693556.96..693556.97 rows=3 width=20) (actual time=17308.521..17308.522 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=324569 read=2284 InitPlan 1 (returns $0) -> Aggregate (cost=52915.24..52915.25 rows=1 width=4) (actual time=1228.206..1228.206 rows=1 loops=1) Buffers: shared hit=35287 read=1056 -> Bitmap Heap Scan on customer (cost=6648.18..51959.66 rows=382228 width=4) (actual time=241.069..1086.455 rows=382007 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{24,23,32,16,21,14,17}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=35287 read=1056 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6552.62 rows=382228 width=0) (actual time=222.899..222.899 rows=382007 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{24,23,32,16,21,14,17}'::text[])) Buffers: shared hit=9 read=1056 -> HashAggregate (cost=640641.65..640641.69 rows=3 width=20) (actual time=17308.441..17308.443 rows=7 loops=1) Buffers: shared hit=324567 read=2284 -> Hash Anti Join (cost=597219.34..640201.41 rows=58699 width=20) (actual time=16689.046..17278.701 rows=63213 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=324567 read=2284 -> Bitmap Heap Scan on customer (cost=2909.50..41863.89 rows=140053 width=24) (actual time=1347.105..1591.319 rows=190800 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{24,23,32,16,21,14,17}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=70467 read=1594 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2874.49 rows=140053 width=0) (actual time=1331.445..1331.445 rows=190800 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{24,23,32,16,21,14,17}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35296 read=1594 -> Hash (cost=405686.04..405686.04 rows=15089904 width=4) (actual time=15326.732..15326.732 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=254097 read=690 -> Seq Scan on orders (cost=0.00..405686.04 rows=15089904 width=4) (actual time=0.032..6728.218 rows=15015000 loops=1) Buffers: shared hit=254097 read=690 Total runtime: 17430.806 ms (30 rows) COMMIT; COMMIT