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 ('24', '23', '11', '30', '32', '20', '25') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('24', '23', '11', '30', '32', '20', '25') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=691016.99..691016.99 rows=3 width=20) (actual time=8497.603..8497.604 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=324000 read=1588 InitPlan 1 (returns $0) -> Aggregate (cost=52809.66..52809.67 rows=1 width=4) (actual time=428.533..428.533 rows=1 loops=1) Buffers: shared hit=35287 read=1052 -> Bitmap Heap Scan on customer customer_1 (cost=6610.88..51860.02 rows=379853 width=4) (actual time=80.406..353.723 rows=381035 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{24,23,11,30,32,20,25}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=35287 read=1052 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6515.92 rows=379853 width=0) (actual time=72.069..72.069 rows=381035 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{24,23,11,30,32,20,25}'::text[])) Buffers: shared hit=9 read=1052 -> HashAggregate (cost=638207.25..638207.30 rows=3 width=20) (actual time=8497.490..8497.493 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=323997 read=1588 -> Hash Anti Join (cost=594262.47..637769.25 rows=58401 width=20) (actual time=8309.083..8479.558 rows=63704 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=323997 read=1588 -> Bitmap Heap Scan on customer (cost=2903.33..41839.27 rows=139350 width=24) (actual time=480.220..561.431 rows=190490 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{24,23,11,30,32,20,25}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35175 Buffers: shared hit=70472 read=1588 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2868.49 rows=139350 width=0) (actual time=472.999..472.999 rows=190490 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{24,23,11,30,32,20,25}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35297 read=1588 -> Hash (cost=403671.84..403671.84 rows=15014984 width=4) (actual time=7783.068..7783.068 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.023..4097.954 rows=15015000 loops=1) Buffers: shared hit=253522 Planning time: 4.256 ms Execution time: 8501.707 ms (34 rows) COMMIT; COMMIT