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', '16', '19', '34', '23', '29', '24') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('25', '16', '19', '34', '23', '29', '24') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=695139.26..695139.27 rows=3 width=20) (actual time=20720.282..20720.283 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=325520 read=1597 InitPlan 1 (returns $0) -> Aggregate (cost=52847.10..52847.11 rows=1 width=4) (actual time=1502.704..1502.705 rows=1 loops=1) Buffers: shared hit=35284 read=1059 -> Bitmap Heap Scan on customer (cost=6633.69..51896.20 rows=380362 width=4) (actual time=279.826..1372.588 rows=381683 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{25,16,19,34,23,29,24}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=35284 read=1059 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6538.60 rows=380362 width=0) (actual time=266.102..266.102 rows=381683 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{25,16,19,34,23,29,24}'::text[])) Buffers: shared hit=9 read=1056 -> HashAggregate (cost=642292.08..642292.12 rows=3 width=20) (actual time=20720.206..20720.206 rows=7 loops=1) Buffers: shared hit=325518 read=1597 -> Hash Anti Join (cost=597805.44..641849.65 rows=58990 width=20) (actual time=20100.612..20693.688 rows=63414 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=325518 read=1597 -> Bitmap Heap Scan on customer (cost=2903.11..41841.23 rows=139433 width=24) (actual time=1660.118..1891.194 rows=190686 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{25,16,19,34,23,29,24}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=70474 read=1597 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2868.26 rows=139433 width=0) (actual time=1642.586..1642.586 rows=190686 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{25,16,19,34,23,29,24}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35294 read=1597 -> Hash (cost=406090.48..406090.48 rows=15104948 width=4) (actual time=18428.831..18428.831 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=255041 -> Seq Scan on orders (cost=0.00..406090.48 rows=15104948 width=4) (actual time=0.022..9093.158 rows=15015000 loops=1) Buffers: shared hit=255041 Total runtime: 20851.576 ms (30 rows) COMMIT; COMMIT