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 ('32', '33', '25', '27', '10', '14', '13') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('32', '33', '25', '27', '10', '14', '13') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694681.22..694681.23 rows=3 width=20) (actual time=10099.684..10099.685 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=265932 read=61156 written=31 InitPlan 1 (returns $0) -> Aggregate (cost=52899.95..52899.96 rows=1 width=4) (actual time=474.445..474.446 rows=1 loops=1) Buffers: shared hit=34995 read=1345 written=1 -> Bitmap Heap Scan on customer customer_1 (cost=6645.80..51945.50 rows=381779 width=4) (actual time=95.063..396.853 rows=381639 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{32,33,25,27,10,14,13}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=34995 read=1345 written=1 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6550.35 rows=381779 width=0) (actual time=86.168..86.168 rows=381639 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{32,33,25,27,10,14,13}'::text[])) Buffers: shared hit=166 read=896 written=1 -> HashAggregate (cost=641781.20..641781.24 rows=3 width=20) (actual time=10099.617..10099.618 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=265929 read=61156 written=31 -> Hash Anti Join (cost=597812.26..641334.82 rows=59517 width=20) (actual time=9776.022..10079.173 rows=63566 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=265929 read=61156 written=31 -> Bitmap Heap Scan on customer (cost=2909.93..41862.72 rows=139992 width=24) (actual time=537.476..723.952 rows=190418 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{32,33,25,27,10,14,13}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35156 Buffers: shared hit=70240 read=1801 written=1 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2874.93 rows=139992 width=0) (actual time=528.786..528.786 rows=190418 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{32,33,25,27,10,14,13}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35084 read=1801 written=1 -> Hash (cost=406090.48..406090.48 rows=15104948 width=4) (actual time=9135.851..9135.851 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=195686 read=59355 written=30 -> Seq Scan on orders (cost=0.00..406090.48 rows=15104948 width=4) (actual time=0.034..4422.259 rows=15015000 loops=1) Buffers: shared hit=195686 read=59355 written=30 Planning time: 2.472 ms Execution time: 10126.817 ms (34 rows) COMMIT; COMMIT