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 ('18', '26', '23', '32', '19', '10', '17') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('18', '26', '23', '32', '19', '10', '17') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=691310.62..691310.63 rows=3 width=20) (actual time=8492.480..8492.481 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=324002 read=1592 InitPlan 1 (returns $0) -> Aggregate (cost=52980.92..52980.93 rows=1 width=4) (actual time=470.880..470.880 rows=1 loops=1) Buffers: shared hit=35287 read=1056 -> Bitmap Heap Scan on customer customer_1 (cost=6681.90..52022.57 rows=383340 width=4) (actual time=87.566..395.725 rows=382133 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,26,23,32,19,10,17}'::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..6586.06 rows=383340 width=0) (actual time=79.312..79.312 rows=382133 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{18,26,23,32,19,10,17}'::text[])) Buffers: shared hit=9 read=1056 -> HashAggregate (cost=638329.62..638329.66 rows=3 width=20) (actual time=8492.376..8492.378 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=323999 read=1592 -> Hash Anti Join (cost=594304.03..637886.89 rows=59031 width=20) (actual time=8266.687..8474.776 rows=63488 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=323999 read=1592 -> Bitmap Heap Scan on customer (cost=2944.89..41915.53 rows=140672 width=24) (actual time=523.302..640.735 rows=190823 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,26,23,32,19,10,17}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35177 Buffers: shared hit=70474 read=1592 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2909.72 rows=140672 width=0) (actual time=516.092..516.092 rows=190823 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,26,23,32,19,10,17}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35297 read=1592 -> Hash (cost=403671.84..403671.84 rows=15014984 width=4) (actual time=7697.853..7697.853 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=253522 -> Seq Scan on orders (cost=0.00..403671.84 rows=15014984 width=4) (actual time=0.024..3993.364 rows=15015000 loops=1) Buffers: shared hit=253522 Planning time: 3.923 ms Execution time: 8496.672 ms (34 rows) COMMIT; COMMIT