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 ('18', '26', '13', '17', '12', '34', '30') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('18', '26', '13', '17', '12', '34', '30') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=695266.81..695266.82 rows=3 width=20) (actual time=18540.395..18540.397 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=323356 read=3757 dirtied=254 written=549 InitPlan 1 (returns $0) -> Aggregate (cost=52919.29..52919.30 rows=1 width=4) (actual time=1250.501..1250.502 rows=1 loops=1) Buffers: shared hit=33458 read=2886 written=488 -> Bitmap Heap Scan on customer (cost=6649.05..51963.44 rows=382339 width=4) (actual time=244.639..1116.850 rows=381763 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,26,13,17,12,34,30}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=33458 read=2886 written=488 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6553.46 rows=382339 width=0) (actual time=224.784..224.784 rows=381763 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{18,26,13,17,12,34,30}'::text[])) Buffers: shared hit=9 read=1057 written=158 -> HashAggregate (cost=642347.44..642347.48 rows=3 width=20) (actual time=18540.325..18540.329 rows=7 loops=1) Buffers: shared hit=323354 read=3757 dirtied=254 written=549 -> Hash Anti Join (cost=597812.91..641902.72 rows=59296 width=20) (actual time=17610.446..18488.760 rows=63771 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=323354 read=3757 dirtied=254 written=549 -> Bitmap Heap Scan on customer (cost=2910.58..41867.72 rows=140158 width=24) (actual time=1429.292..1939.840 rows=190819 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,26,13,17,12,34,30}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=68642 read=3425 written=549 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2875.54 rows=140158 width=0) (actual time=1411.987..1411.987 rows=190819 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,26,13,17,12,34,30}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=33468 read=3424 written=549 -> Hash (cost=406090.48..406090.48 rows=15104948 width=4) (actual time=16169.810..16169.810 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=254709 read=332 dirtied=254 -> Seq Scan on orders (cost=0.00..406090.48 rows=15104948 width=4) (actual time=0.020..7824.742 rows=15015000 loops=1) Buffers: shared hit=254709 read=332 dirtied=254 Total runtime: 18623.226 ms (30 rows) COMMIT; COMMIT