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 ('17', '34', '28', '15', '31', '19', '13') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('17', '34', '28', '15', '31', '19', '13') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694112.53..694112.53 rows=3 width=20) (actual time=11830.866..11830.867 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=253309 read=73539 dirtied=207 written=59 InitPlan 1 (returns $0) -> Aggregate (cost=52913.74..52913.75 rows=1 width=4) (actual time=580.505..580.505 rows=1 loops=1) Buffers: shared hit=30398 read=5946 -> Bitmap Heap Scan on customer customer_1 (cost=6648.73..51958.35 rows=382157 width=4) (actual time=97.576..502.887 rows=382615 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{17,34,28,15,31,19,13}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=30398 read=5946 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6553.19 rows=382157 width=0) (actual time=88.890..88.890 rows=382615 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{17,34,28,15,31,19,13}'::text[])) Buffers: shared hit=6 read=1060 -> HashAggregate (cost=641198.70..641198.75 rows=3 width=20) (actual time=11830.796..11830.798 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=253306 read=73539 dirtied=207 written=59 -> Hash Anti Join (cost=597221.21..640751.88 rows=59576 width=20) (actual time=11366.732..11809.012 rows=63454 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=253306 read=73539 dirtied=207 written=59 -> Bitmap Heap Scan on customer (cost=2911.37..41867.78 rows=140130 width=24) (actual time=647.041..934.714 rows=191060 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{17,34,28,15,31,19,13}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35166 Buffers: shared hit=60619 read=11436 written=5 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2876.33 rows=140130 width=0) (actual time=638.046..638.046 rows=191060 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{17,34,28,15,31,19,13}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=30407 read=6482 -> Hash (cost=405686.04..405686.04 rows=15089904 width=4) (actual time=10569.920..10569.920 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=192684 read=62103 dirtied=207 written=54 -> Seq Scan on orders (cost=0.00..405686.04 rows=15089904 width=4) (actual time=0.017..4823.019 rows=15015000 loops=1) Buffers: shared hit=192684 read=62103 dirtied=207 written=54 Planning time: 2.993 ms Execution time: 11854.179 ms (34 rows) COMMIT; COMMIT