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 ('33', '12', '13', '31', '17', '21', '20') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('33', '12', '13', '31', '17', '21', '20') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=692356.07..692356.08 rows=3 width=20) (actual time=13768.561..13768.562 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=190082 read=136003 dirtied=252 written=33 InitPlan 1 (returns $0) -> Aggregate (cost=52917.32..52917.33 rows=1 width=4) (actual time=1198.262..1198.263 rows=1 loops=1) Buffers: shared hit=12 read=36331 -> Bitmap Heap Scan on customer customer_1 (cost=6649.49..51961.68 rows=382255 width=4) (actual time=148.605..1071.975 rows=382403 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{33,12,13,31,17,21,20}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=12 read=36331 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6553.92 rows=382255 width=0) (actual time=131.466..131.466 rows=382403 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{33,12,13,31,17,21,20}'::text[])) Buffers: shared hit=10 read=1055 -> HashAggregate (cost=639438.67..639438.72 rows=3 width=20) (actual time=13768.477..13768.480 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=190079 read=136003 dirtied=252 written=33 -> Hash Anti Join (cost=595450.72..638996.12 rows=59007 width=20) (actual time=12902.204..13742.283 rows=63741 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=190079 read=136003 dirtied=252 written=33 -> Bitmap Heap Scan on customer (cost=2911.30..41867.66 rows=140128 width=24) (actual time=1302.995..1932.706 rows=191027 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{33,12,13,31,17,21,20}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35160 Buffers: shared hit=24 read=72027 written=19 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2876.26 rows=140128 width=0) (actual time=1290.312..1290.312 rows=191027 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{33,12,13,31,17,21,20}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=22 read=36869 -> Hash (cost=404477.52..404477.52 rows=15044952 width=4) (actual time=11541.491..11541.491 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=190052 read=63976 dirtied=252 written=14 -> Seq Scan on orders (cost=0.00..404477.52 rows=15044952 width=4) (actual time=0.035..5945.182 rows=15015000 loops=1) Buffers: shared hit=190052 read=63976 dirtied=252 written=14 Planning time: 4.549 ms Execution time: 13776.509 ms (34 rows) COMMIT; COMMIT