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 ('34', '20', '31', '17', '28', '16', '18') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('34', '20', '31', '17', '28', '16', '18') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=695314.90..695314.91 rows=3 width=20) (actual time=12954.922..12954.924 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=170068 read=157300 written=133 InitPlan 1 (returns $0) -> Aggregate (cost=52919.31..52919.32 rows=1 width=4) (actual time=887.300..887.300 rows=1 loops=1) Buffers: shared hit=11 read=36332 written=29 -> Bitmap Heap Scan on customer customer_1 (cost=6649.92..51963.53 rows=382309 width=4) (actual time=106.784..809.193 rows=382371 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{34,20,31,17,28,16,18}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35277 Buffers: shared hit=11 read=36332 written=29 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6554.34 rows=382309 width=0) (actual time=97.773..97.773 rows=382371 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{34,20,31,17,28,16,18}'::text[])) Buffers: shared hit=9 read=1057 -> HashAggregate (cost=642395.51..642395.56 rows=3 width=20) (actual time=12954.846..12954.847 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=170065 read=157300 written=133 -> Hash Anti Join (cost=598406.31..641952.90 rows=59015 width=20) (actual time=12570.822..12933.788 rows=63697 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=170065 read=157300 written=133 -> Bitmap Heap Scan on customer (cost=2911.51..41868.40 rows=140148 width=24) (actual time=954.976..1184.065 rows=191243 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{34,20,31,17,28,16,18}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35176 Buffers: shared hit=35197 read=36870 written=30 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2876.47 rows=140148 width=0) (actual time=945.817..945.817 rows=191243 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{34,20,31,17,28,16,18}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=21 read=36870 written=30 -> Hash (cost=406494.91..406494.91 rows=15119991 width=4) (actual time=11484.947..11484.947 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=134865 read=120430 written=103 -> Seq Scan on orders (cost=0.00..406494.91 rows=15119991 width=4) (actual time=0.024..5920.450 rows=15015000 loops=1) Buffers: shared hit=134865 read=120430 written=103 Planning time: 5.917 ms Execution time: 12976.157 ms (34 rows) COMMIT; COMMIT