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 ('18', '10', '13', '21', '17', '19', '29') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('18', '10', '13', '21', '17', '19', '29') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694096.30..694096.31 rows=3 width=20) (actual time=9855.155..9855.155 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=291075 read=35782 dirtied=1 written=460 InitPlan 1 (returns $0) -> Aggregate (cost=52892.51..52892.52 rows=1 width=4) (actual time=547.197..547.197 rows=1 loops=1) Buffers: shared hit=35288 read=1055 written=59 -> Bitmap Heap Scan on customer customer_1 (cost=6644.23..51938.57 rows=381575 width=4) (actual time=97.911..467.121 rows=382408 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,10,13,21,17,19,29}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=35288 read=1055 written=59 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6548.83 rows=381575 width=0) (actual time=89.066..89.066 rows=382408 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{18,10,13,21,17,19,29}'::text[])) Buffers: shared hit=10 read=1055 written=59 -> HashAggregate (cost=641203.71..641203.76 rows=3 width=20) (actual time=9855.095..9855.097 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=291072 read=35782 dirtied=1 written=460 -> Hash Anti Join (cost=597219.63..640763.72 rows=58666 width=20) (actual time=9491.747..9834.184 rows=63774 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=291072 read=35782 dirtied=1 written=460 -> Bitmap Heap Scan on customer (cost=2909.79..41862.32 rows=139982 width=24) (actual time=611.927..843.822 rows=190704 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,10,13,21,17,19,29}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35176 Buffers: shared hit=69534 read=2530 written=104 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2874.79 rows=139982 width=0) (actual time=603.025..603.025 rows=190704 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,10,13,21,17,19,29}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35299 read=1589 written=104 -> Hash (cost=405686.04..405686.04 rows=15089904 width=4) (actual time=8796.544..8796.544 rows=15000000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658416kB Buffers: shared hit=221535 read=33252 dirtied=1 written=356 -> Seq Scan on orders (cost=0.00..405686.04 rows=15089904 width=4) (actual time=0.021..4585.182 rows=15000000 loops=1) Buffers: shared hit=221535 read=33252 dirtied=1 written=356 Planning time: 2.663 ms Execution time: 9859.733 ms (34 rows) COMMIT; COMMIT