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 ('25', '15', '17', '33', '10', '28', '18') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('25', '15', '17', '33', '10', '28', '18') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=696027.51..696027.52 rows=3 width=20) (actual time=16518.198..16518.198 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=282935 read=44664 dirtied=252 written=4 InitPlan 1 (returns $0) -> Aggregate (cost=52956.67..52956.68 rows=1 width=4) (actual time=1503.711..1503.712 rows=1 loops=1) Buffers: shared hit=15 read=36329 -> Bitmap Heap Scan on customer (cost=6675.87..51999.90 rows=382706 width=4) (actual time=207.235..1377.362 rows=382602 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{25,15,17,33,10,28,18}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=15 read=36329 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6580.19 rows=382706 width=0) (actual time=178.007..178.007 rows=382602 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{25,15,17,33,10,28,18}'::text[])) Buffers: shared hit=7 read=1059 -> HashAggregate (cost=643070.76..643070.81 rows=3 width=20) (actual time=16518.113..16518.115 rows=7 loops=1) Buffers: shared hit=282933 read=44664 dirtied=252 written=4 -> Hash Anti Join (cost=599025.38..642627.45 rows=59108 width=20) (actual time=15901.740..16466.259 rows=63733 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=282933 read=44664 dirtied=252 written=4 -> Bitmap Heap Scan on customer (cost=2940.44..41902.42 rows=140342 width=24) (actual time=1690.619..1927.208 rows=190958 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{25,15,17,33,10,28,18}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35180 read=36866 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2905.36 rows=140342 width=0) (actual time=1661.855..1661.855 rows=190958 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{25,15,17,33,10,28,18}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=25 read=36866 -> Hash (cost=406897.75..406897.75 rows=15134975 width=4) (actual time=14195.837..14195.837 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=247750 read=7798 dirtied=252 written=4 -> Seq Scan on orders (cost=0.00..406897.75 rows=15134975 width=4) (actual time=0.046..7228.750 rows=15015000 loops=1) Buffers: shared hit=247750 read=7798 dirtied=252 written=4 Total runtime: 16601.649 ms (30 rows) COMMIT; COMMIT