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 ('15', '24', '34', '21', '32', '14', '30') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('15', '24', '34', '21', '32', '14', '30') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694843.51..694843.51 rows=3 width=20) (actual time=22501.169..22501.170 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=278460 read=48628 written=1675 InitPlan 1 (returns $0) -> Aggregate (cost=52957.18..52957.19 rows=1 width=4) (actual time=931.529..931.529 rows=1 loops=1) Buffers: shared hit=35293 read=1050 -> Bitmap Heap Scan on customer (cost=6675.98..52000.38 rows=382720 width=4) (actual time=204.285..809.991 rows=382082 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{15,24,34,21,32,14,30}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=35293 read=1050 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6580.30 rows=382720 width=0) (actual time=188.351..188.351 rows=382082 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{15,24,34,21,32,14,30}'::text[])) Buffers: shared hit=15 read=1050 -> HashAggregate (cost=641886.25..641886.29 rows=3 width=20) (actual time=22501.107..22501.109 rows=7 loops=1) Buffers: shared hit=278458 read=48628 written=1675 -> Hash Anti Join (cost=597843.71..641438.96 rows=59639 width=20) (actual time=21382.197..22468.816 rows=63605 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=278458 read=48628 written=1675 -> Bitmap Heap Scan on customer (cost=2941.38..41905.85 rows=140437 width=24) (actual time=1101.967..1815.497 rows=190884 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{15,24,34,21,32,14,30}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35308 read=36734 written=1671 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2906.27 rows=140437 width=0) (actual time=1084.567..1084.567 rows=190884 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{15,24,34,21,32,14,30}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35306 read=1584 -> Hash (cost=406090.48..406090.48 rows=15104948 width=4) (actual time=20261.877..20261.877 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=243147 read=11894 written=4 -> Seq Scan on orders (cost=0.00..406090.48 rows=15104948 width=4) (actual time=0.029..7208.293 rows=15015000 loops=1) Buffers: shared hit=243147 read=11894 written=4 Total runtime: 22655.991 ms (30 rows) COMMIT; COMMIT