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 ('22', '13', '15', '18', '32', '30', '19') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('22', '13', '15', '18', '32', '30', '19') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=693449.07..693449.08 rows=3 width=20) (actual time=29141.170..29141.170 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=280867 read=45744 InitPlan 1 (returns $0) -> Aggregate (cost=52847.24..52847.25 rows=1 width=4) (actual time=3932.263..3932.263 rows=1 loops=1) Buffers: shared hit=11 read=36335 -> Bitmap Heap Scan on customer (cost=6633.75..51896.33 rows=380365 width=4) (actual time=282.396..3795.466 rows=382476 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{22,13,15,18,32,30,19}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=11 read=36335 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6538.66 rows=380365 width=0) (actual time=271.069..271.069 rows=382476 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{22,13,15,18,32,30,19}'::text[])) Buffers: shared hit=8 read=1060 -> HashAggregate (cost=640601.75..640601.79 rows=3 width=20) (actual time=29141.061..29141.066 rows=7 loops=1) Buffers: shared hit=280865 read=45744 -> Hash Anti Join (cost=596622.89..640156.93 rows=59309 width=20) (actual time=27633.014..29084.457 rows=63745 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=280865 read=45744 -> Bitmap Heap Scan on customer (cost=2903.19..41841.57 rows=139443 width=24) (actual time=4064.164..5049.944 rows=190891 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{22,13,15,18,32,30,19}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35196 read=36876 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2868.33 rows=139443 width=0) (actual time=4050.879..4050.879 rows=190891 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{22,13,15,18,32,30,19}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=20 read=36873 -> Hash (cost=405283.20..405283.20 rows=15074920 width=4) (actual time=23559.497..23559.497 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=245666 read=8868 -> Seq Scan on orders (cost=0.00..405283.20 rows=15074920 width=4) (actual time=0.038..13379.919 rows=15015000 loops=1) Buffers: shared hit=245666 read=8868 Total runtime: 29312.290 ms (30 rows) COMMIT; COMMIT