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 ('23', '10', '16', '33', '11', '24', '32') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('23', '10', '16', '33', '11', '24', '32') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=693406.29..693406.29 rows=3 width=20) (actual time=9544.322..9544.323 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=258207 read=68393 dirtied=1 written=6144 InitPlan 1 (returns $0) -> Aggregate (cost=52833.77..52833.78 rows=1 width=4) (actual time=444.673..444.673 rows=1 loops=1) Buffers: shared hit=35441 read=900 -> Bitmap Heap Scan on customer customer_1 (cost=6631.75..51883.86 rows=379966 width=4) (actual time=85.517..372.636 rows=381607 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{23,10,16,33,11,24,32}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=35441 read=900 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6536.76 rows=379966 width=0) (actual time=77.178..77.178 rows=381607 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{23,10,16,33,11,24,32}'::text[])) Buffers: shared hit=163 read=900 -> HashAggregate (cost=640572.43..640572.48 rows=3 width=20) (actual time=9544.269..9544.270 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=258204 read=68393 dirtied=1 written=6144 -> Hash Anti Join (cost=596623.89..640133.60 rows=58511 width=20) (actual time=9213.634..9523.483 rows=63310 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=258204 read=68393 dirtied=1 written=6144 -> Bitmap Heap Scan on customer (cost=2904.19..41842.31 rows=139433 width=24) (actual time=502.417..703.387 rows=190592 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{23,10,16,33,11,24,32}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35173 Buffers: shared hit=70702 read=1358 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2869.33 rows=139433 width=0) (actual time=494.041..494.041 rows=190592 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{23,10,16,33,11,24,32}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35530 read=1357 -> Hash (cost=405283.20..405283.20 rows=15074920 width=4) (actual time=8646.461..8646.461 rows=15000000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658416kB Buffers: shared hit=187499 read=67035 dirtied=1 written=6144 -> Seq Scan on orders (cost=0.00..405283.20 rows=15074920 width=4) (actual time=0.019..4493.072 rows=15000000 loops=1) Buffers: shared hit=187499 read=67035 dirtied=1 written=6144 Planning time: 2.387 ms Execution time: 9549.395 ms (34 rows) COMMIT; COMMIT