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 ('24', '27', '20', '18', '25', '21', '32') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('24', '27', '20', '18', '25', '21', '32') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=697155.36..697155.37 rows=3 width=20) (actual time=12123.698..12123.698 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=221261 read=106857 dirtied=62 written=56 InitPlan 1 (returns $0) -> Aggregate (cost=52945.44..52945.45 rows=1 width=4) (actual time=793.260..793.260 rows=1 loops=1) Buffers: shared hit=12 read=36327 written=41 -> Bitmap Heap Scan on customer customer_1 (cost=6674.36..51989.52 rows=382368 width=4) (actual time=91.824..722.844 rows=381648 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{24,27,20,18,25,21,32}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35277 Buffers: shared hit=12 read=36327 written=41 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6578.76 rows=382368 width=0) (actual time=83.212..83.212 rows=381648 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{24,27,20,18,25,21,32}'::text[])) Buffers: shared hit=10 read=1052 -> HashAggregate (cost=644209.84..644209.89 rows=3 width=20) (actual time=12123.640..12123.643 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=221258 read=106857 dirtied=62 written=56 -> Hash Anti Join (cost=600206.45..643768.23 rows=58881 width=20) (actual time=11536.427..12100.395 rows=63683 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=221258 read=106857 dirtied=62 written=56 -> Bitmap Heap Scan on customer (cost=2941.23..41902.50 rows=140315 width=24) (actual time=858.520..1285.583 rows=190564 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{24,27,20,18,25,21,32}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35172 Buffers: shared hit=12031 read=60027 written=54 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2906.15 rows=140315 width=0) (actual time=849.420..849.420 rows=190564 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{24,27,20,18,25,21,32}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=23 read=36863 written=41 -> Hash (cost=407703.43..407703.43 rows=15164943 width=4) (actual time=9952.240..9952.240 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=209224 read=46830 dirtied=62 written=2 -> Seq Scan on orders (cost=0.00..407703.43 rows=15164943 width=4) (actual time=0.035..4693.099 rows=15015000 loops=1) Buffers: shared hit=209224 read=46830 dirtied=62 written=2 Planning time: 2.853 ms Execution time: 12139.825 ms (34 rows) COMMIT; COMMIT