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 ('22', '27', '30', '33', '29', '32', '14') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('22', '27', '30', '33', '29', '32', '14') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694187.55..694187.56 rows=3 width=20) (actual time=17444.076..17444.077 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=269188 read=57667 dirtied=253 written=15 InitPlan 1 (returns $0) -> Aggregate (cost=52929.86..52929.87 rows=1 width=4) (actual time=1135.551..1135.551 rows=1 loops=1) Buffers: shared hit=25 read=36318 -> Bitmap Heap Scan on customer (cost=6651.27..51973.28 rows=382629 width=4) (actual time=173.519..1020.813 rows=381554 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{22,27,30,33,29,32,14}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=25 read=36318 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6555.61 rows=382629 width=0) (actual time=153.912..153.912 rows=381554 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{22,27,30,33,29,32,14}'::text[])) Buffers: shared hit=8 read=1057 -> HashAggregate (cost=641257.62..641257.66 rows=3 width=20) (actual time=17444.000..17444.003 rows=7 loops=1) Buffers: shared hit=269186 read=57667 dirtied=253 written=15 -> Hash Anti Join (cost=597221.06..640814.13 rows=59132 width=20) (actual time=16590.867..17398.113 rows=63879 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=269186 read=57667 dirtied=253 written=15 -> Bitmap Heap Scan on customer (cost=2911.22..41870.05 rows=140222 width=24) (actual time=1243.239..1615.590 rows=190641 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{22,27,30,33,29,32,14}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35207 read=36856 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2876.17 rows=140222 width=0) (actual time=1229.877..1229.877 rows=190641 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{22,27,30,33,29,32,14}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=36 read=36855 -> Hash (cost=405686.04..405686.04 rows=15089904 width=4) (actual time=15334.292..15334.292 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=233976 read=20811 dirtied=253 written=15 -> Seq Scan on orders (cost=0.00..405686.04 rows=15089904 width=4) (actual time=0.033..6838.568 rows=15015000 loops=1) Buffers: shared hit=233976 read=20811 dirtied=253 written=15 Total runtime: 17575.901 ms (30 rows) COMMIT; COMMIT