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', '14', '17', '20', '15', '28', '31') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('18', '14', '17', '20', '15', '28', '31') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=691854.37..691854.38 rows=3 width=20) (actual time=10023.089..10023.090 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=322243 read=3601 InitPlan 1 (returns $0) -> Aggregate (cost=52962.36..52962.37 rows=1 width=4) (actual time=537.736..537.736 rows=1 loops=1) Buffers: shared hit=35740 read=604 -> Bitmap Heap Scan on customer customer_1 (cost=6677.94..52005.28 rows=382832 width=4) (actual time=92.931..457.926 rows=382499 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,14,17,20,15,28,31}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=35740 read=604 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6582.23 rows=382832 width=0) (actual time=83.509..83.509 rows=382499 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{18,14,17,20,15,28,31}'::text[])) Buffers: shared hit=462 read=604 -> HashAggregate (cost=638891.94..638891.98 rows=3 width=20) (actual time=10023.035..10023.037 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=322240 read=3601 -> Hash Anti Join (cost=594890.80..638448.72 rows=59096 width=20) (actual time=9657.155..10002.372 rows=63660 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=322240 read=3601 -> Bitmap Heap Scan on customer (cost=2941.52..41903.44 rows=140340 width=24) (actual time=603.043..833.379 rows=191111 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,14,17,20,15,28,31}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35172 Buffers: shared hit=71142 read=921 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2906.43 rows=140340 width=0) (actual time=593.886..593.886 rows=191111 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,14,17,20,15,28,31}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35981 read=910 -> Hash (cost=404074.68..404074.68 rows=15029968 width=4) (actual time=9000.249..9000.249 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=251095 read=2680 -> Seq Scan on orders (cost=0.00..404074.68 rows=15029968 width=4) (actual time=0.020..4695.190 rows=15015000 loops=1) Buffers: shared hit=251095 read=2680 Planning time: 2.594 ms Execution time: 10028.420 ms (34 rows) COMMIT; COMMIT