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 ('21', '19', '22', '13', '24', '33', '12') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('21', '19', '22', '13', '24', '33', '12') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=693557.96..693557.97 rows=3 width=20) (actual time=8799.827..8799.827 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=250809 read=75787 InitPlan 1 (returns $0) -> Aggregate (cost=52928.79..52928.80 rows=1 width=4) (actual time=412.256..412.257 rows=1 loops=1) Buffers: shared hit=35287 read=1056 -> Bitmap Heap Scan on customer customer_1 (cost=6651.93..51972.36 rows=382569 width=4) (actual time=102.267..341.117 rows=381905 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{21,19,22,13,24,33,12}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=35287 read=1056 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6556.29 rows=382569 width=0) (actual time=93.338..93.338 rows=381905 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{21,19,22,13,24,33,12}'::text[])) Buffers: shared hit=9 read=1056 -> HashAggregate (cost=640629.09..640629.13 rows=3 width=20) (actual time=8799.780..8799.780 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=250806 read=75787 -> Hash Anti Join (cost=596632.43..640186.24 rows=59047 width=20) (actual time=8578.319..8779.175 rows=63383 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=250806 read=75787 -> Bitmap Heap Scan on customer (cost=2912.73..41872.63 rows=140263 width=24) (actual time=477.926..575.301 rows=190546 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{21,19,22,13,24,33,12}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35167 Buffers: shared hit=70463 read=1593 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2877.66 rows=140263 width=0) (actual time=469.256..469.256 rows=190546 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{21,19,22,13,24,33,12}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35296 read=1593 -> Hash (cost=405283.20..405283.20 rows=15074920 width=4) (actual time=8055.136..8055.136 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=180340 read=74194 -> Seq Scan on orders (cost=0.00..405283.20 rows=15074920 width=4) (actual time=0.011..4054.694 rows=15015000 loops=1) Buffers: shared hit=180340 read=74194 Planning time: 2.892 ms Execution time: 8804.104 ms (34 rows) COMMIT; COMMIT