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 ('28', '12', '32', '26', '29', '25', '30') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('28', '12', '32', '26', '29', '25', '30') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=697641.94..697641.95 rows=3 width=20) (actual time=10104.511..10104.511 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=286553 read=41836 dirtied=62 written=75 InitPlan 1 (returns $0) -> Aggregate (cost=52904.41..52904.42 rows=1 width=4) (actual time=760.113..760.113 rows=1 loops=1) Buffers: shared hit=12 read=36329 -> Bitmap Heap Scan on customer customer_1 (cost=6646.72..51949.65 rows=381902 width=4) (actual time=90.382..690.023 rows=381536 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{28,12,32,26,29,25,30}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=12 read=36329 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6551.25 rows=381902 width=0) (actual time=81.606..81.606 rows=381536 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{28,12,32,26,29,25,30}'::text[])) Buffers: shared hit=10 read=1053 -> HashAggregate (cost=644737.45..644737.50 rows=3 width=20) (actual time=10104.442..10104.443 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=286550 read=41836 dirtied=62 written=75 -> Hash Anti Join (cost=600765.72..644290.93 rows=59536 width=20) (actual time=9751.454..10082.891 rows=63836 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=286550 read=41836 dirtied=62 written=75 -> Bitmap Heap Scan on customer (cost=2910.36..41864.33 rows=140037 width=24) (actual time=818.924..1028.448 rows=190871 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{28,12,32,26,29,25,30}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35187 Buffers: shared hit=35206 read=36870 written=1 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2875.35 rows=140037 width=0) (actual time=810.831..810.831 rows=190871 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{28,12,32,26,29,25,30}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=22 read=36867 -> Hash (cost=408106.27..408106.27 rows=15179927 width=4) (actual time=8800.336..8800.336 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=251341 read=4966 dirtied=62 written=74 -> Seq Scan on orders (cost=0.00..408106.27 rows=15179927 width=4) (actual time=0.022..4127.081 rows=15015000 loops=1) Buffers: shared hit=251341 read=4966 dirtied=62 written=74 Planning time: 3.117 ms Execution time: 10117.652 ms (34 rows) COMMIT; COMMIT