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 ('34', '20', '13', '18', '21', '29', '12') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('34', '20', '13', '18', '21', '29', '12') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=693552.86..693552.87 rows=3 width=20) (actual time=11140.542..11140.543 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=252133 read=74458 written=22 InitPlan 1 (returns $0) -> Aggregate (cost=52919.56..52919.57 rows=1 width=4) (actual time=542.471..542.471 rows=1 loops=1) Buffers: shared hit=35163 read=1180 -> Bitmap Heap Scan on customer customer_1 (cost=6649.98..51963.77 rows=382316 width=4) (actual time=105.698..465.702 rows=382165 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{34,20,13,18,21,29,12}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=35163 read=1180 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6554.40 rows=382316 width=0) (actual time=96.339..96.339 rows=382165 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{34,20,13,18,21,29,12}'::text[])) Buffers: shared hit=10 read=1055 -> HashAggregate (cost=640633.22..640633.26 rows=3 width=20) (actual time=11140.485..11140.487 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=252130 read=74458 written=22 -> Hash Anti Join (cost=596632.29..640192.37 rows=58780 width=20) (actual time=10388.594..11115.522 rows=63866 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=252130 read=74458 written=22 -> Bitmap Heap Scan on customer (cost=2912.59..41872.23 rows=140253 width=24) (actual time=606.004..1181.662 rows=190856 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{34,20,13,18,21,29,12}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35160 Buffers: shared hit=35175 read=36876 written=16 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2877.52 rows=140253 width=0) (actual time=597.364..597.364 rows=190856 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{34,20,13,18,21,29,12}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35173 read=1718 -> Hash (cost=405283.20..405283.20 rows=15074920 width=4) (actual time=9668.288..9668.288 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=216952 read=37582 written=6 -> Seq Scan on orders (cost=0.00..405283.20 rows=15074920 width=4) (actual time=0.025..4903.187 rows=15015000 loops=1) Buffers: shared hit=216952 read=37582 written=6 Planning time: 3.353 ms Execution time: 11157.667 ms (34 rows) COMMIT; COMMIT