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 ('20', '30', '27', '17', '21', '15', '16') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('20', '30', '27', '17', '21', '15', '16') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=693546.97..693546.98 rows=3 width=20) (actual time=11305.215..11305.215 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=281853 read=44765 dirtied=94 written=10 InitPlan 1 (returns $0) -> Aggregate (cost=52915.14..52915.15 rows=1 width=4) (actual time=778.366..778.366 rows=1 loops=1) Buffers: shared hit=14 read=36329 -> Bitmap Heap Scan on customer customer_1 (cost=6649.00..51959.65 rows=382196 width=4) (actual time=90.069..706.901 rows=382517 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{20,30,27,17,21,15,16}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=14 read=36329 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6553.45 rows=382196 width=0) (actual time=81.601..81.601 rows=382517 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{20,30,27,17,21,15,16}'::text[])) Buffers: shared hit=11 read=1054 -> HashAggregate (cost=640631.75..640631.80 rows=3 width=20) (actual time=11305.140..11305.142 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=281850 read=44765 dirtied=94 written=10 -> Hash Anti Join (cost=596632.29..640190.34 rows=58855 width=20) (actual time=10927.488..11284.467 rows=63744 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=281850 read=44765 dirtied=94 written=10 -> Bitmap Heap Scan on customer (cost=2912.59..41872.20 rows=140252 width=24) (actual time=836.360..1063.169 rows=191237 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{20,30,27,17,21,15,16}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35188 Buffers: shared hit=35108 read=36970 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2877.52 rows=140252 width=0) (actual time=828.366..828.366 rows=191237 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{20,30,27,17,21,15,16}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=25 read=36865 -> Hash (cost=405283.20..405283.20 rows=15074920 width=4) (actual time=9986.958..9986.958 rows=15000000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658416kB Buffers: shared hit=246739 read=7795 dirtied=94 written=10 -> Seq Scan on orders (cost=0.00..405283.20 rows=15074920 width=4) (actual time=0.021..4589.985 rows=15000000 loops=1) Buffers: shared hit=246739 read=7795 dirtied=94 written=10 Planning time: 2.868 ms Execution time: 11310.510 ms (34 rows) COMMIT; COMMIT