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', '11', '12', '18', '30', '25', '24') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('15', '11', '12', '18', '30', '25', '24') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=695197.65..695197.66 rows=3 width=20) (actual time=13563.224..13563.224 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=318494 read=9112 InitPlan 1 (returns $0) -> Aggregate (cost=52833.79..52833.80 rows=1 width=4) (actual time=746.164..746.164 rows=1 loops=1) Buffers: shared hit=31981 read=4361 -> Bitmap Heap Scan on customer (cost=6630.87..51883.79 rows=379997 width=4) (actual time=144.644..636.405 rows=381449 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{15,11,12,18,30,25,24}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=31981 read=4361 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6535.87 rows=379997 width=0) (actual time=132.341..132.341 rows=381449 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{15,11,12,18,30,25,24}'::text[])) Buffers: shared hit=9 read=1055 -> HashAggregate (cost=642363.78..642363.83 rows=3 width=20) (actual time=13563.148..13563.151 rows=7 loops=1) Buffers: shared hit=318492 read=9112 -> Hash Anti Join (cost=598987.55..641920.50 rows=59104 width=20) (actual time=12888.880..13525.740 rows=63662 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=318492 read=9112 -> Bitmap Heap Scan on customer (cost=2902.61..41839.42 rows=139383 width=24) (actual time=838.946..1092.245 rows=190459 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{15,11,12,18,30,25,24}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=63145 read=8908 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2867.77 rows=139383 width=0) (actual time=823.706..823.706 rows=190459 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{15,11,12,18,30,25,24}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=31991 read=4898 -> Hash (cost=406897.75..406897.75 rows=15134975 width=4) (actual time=12038.762..12038.762 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=255344 read=204 -> Seq Scan on orders (cost=0.00..406897.75 rows=15134975 width=4) (actual time=0.024..4949.455 rows=15015000 loops=1) Buffers: shared hit=255344 read=204 Total runtime: 13684.925 ms (30 rows) COMMIT; COMMIT