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 ('20', '29', '28', '14', '27', '31', '21') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('20', '29', '28', '14', '27', '31', '21') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=695907.11..695907.12 rows=3 width=20) (actual time=9454.984..9454.985 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=314524 read=13081 InitPlan 1 (returns $0) -> Aggregate (cost=52918.03..52918.04 rows=1 width=4) (actual time=469.651..469.651 rows=1 loops=1) Buffers: shared hit=25393 read=10948 -> Bitmap Heap Scan on customer customer_1 (cost=6649.65..51962.34 rows=382274 width=4) (actual time=78.511..400.371 rows=381692 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{20,29,28,14,27,31,21}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=25393 read=10948 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6554.08 rows=382274 width=0) (actual time=70.708..70.708 rows=381692 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{20,29,28,14,27,31,21}'::text[])) Buffers: shared hit=11 read=1052 -> HashAggregate (cost=642989.01..642989.05 rows=3 width=20) (actual time=9454.908..9454.910 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=314521 read=13081 -> Hash Anti Join (cost=598996.45..642547.74 rows=58835 width=20) (actual time=9180.326..9436.298 rows=63696 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=314521 read=13081 -> Bitmap Heap Scan on customer (cost=2911.51..41868.40 rows=140148 width=24) (actual time=525.812..647.065 rows=190793 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{20,29,28,14,27,31,21}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35163 Buffers: shared hit=60567 read=11484 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2876.47 rows=140148 width=0) (actual time=517.882..517.882 rows=190793 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{20,29,28,14,27,31,21}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=25404 read=11484 -> Hash (cost=406897.75..406897.75 rows=15134975 width=4) (actual time=8547.659..8547.659 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=253951 read=1597 -> Seq Scan on orders (cost=0.00..406897.75 rows=15134975 width=4) (actual time=0.027..3780.960 rows=15015000 loops=1) Buffers: shared hit=253951 read=1597 Planning time: 2.258 ms Execution time: 9489.252 ms (34 rows) COMMIT; COMMIT