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 ('26', '11', '15', '10', '12', '14', '31') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('26', '11', '15', '10', '12', '14', '31') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694655.99..694655.99 rows=3 width=20) (actual time=16358.137..16358.139 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=200575 read=126537 dirtied=93 written=5377 InitPlan 1 (returns $0) -> Aggregate (cost=52872.58..52872.59 rows=1 width=4) (actual time=701.581..701.581 rows=1 loops=1) Buffers: shared hit=14 read=36327 -> Bitmap Heap Scan on customer customer_1 (cost=6639.99..51920.00 rows=381029 width=4) (actual time=80.552..635.410 rows=381525 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{26,11,15,10,12,14,31}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=14 read=36327 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6544.74 rows=381029 width=0) (actual time=72.827..72.827 rows=381525 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{26,11,15,10,12,14,31}'::text[])) Buffers: shared hit=11 read=1052 -> HashAggregate (cost=641783.33..641783.37 rows=3 width=20) (actual time=16358.073..16358.074 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=200572 read=126537 dirtied=93 written=5377 -> Hash Anti Join (cost=597810.54..641343.27 rows=58675 width=20) (actual time=15682.935..16334.252 rows=63238 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=200572 read=126537 dirtied=93 written=5377 -> Bitmap Heap Scan on customer (cost=2908.21..41856.56 rows=139823 width=24) (actual time=755.566..1229.672 rows=190686 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{26,11,15,10,12,14,31}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35178 Buffers: shared hit=7757 read=64308 written=2851 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2873.25 rows=139823 width=0) (actual time=747.962..747.962 rows=190686 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{26,11,15,10,12,14,31}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=24 read=36863 -> Hash (cost=406090.48..406090.48 rows=15104948 width=4) (actual time=11635.901..11635.901 rows=15000000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658416kB Buffers: shared hit=192812 read=62229 dirtied=93 written=2526 -> Seq Scan on orders (cost=0.00..406090.48 rows=15104948 width=4) (actual time=0.251..5178.308 rows=15000000 loops=1) Buffers: shared hit=192812 read=62229 dirtied=93 written=2526 Planning time: 2.753 ms Execution time: 16384.878 ms (34 rows) COMMIT; COMMIT