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', '32', '12', '13', '24', '25', '14') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('15', '32', '12', '13', '24', '25', '14') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694697.55..694697.56 rows=3 width=20) (actual time=9836.981..9836.981 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=203921 read=123194 dirtied=128 written=6167 InitPlan 1 (returns $0) -> Aggregate (cost=52902.36..52902.37 rows=1 width=4) (actual time=648.179..648.179 rows=1 loops=1) Buffers: shared hit=15 read=36326 -> Bitmap Heap Scan on customer customer_1 (cost=6646.29..51947.75 rows=381846 width=4) (actual time=79.182..582.110 rows=381772 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{15,32,12,13,24,25,14}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=15 read=36326 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6550.83 rows=381846 width=0) (actual time=71.638..71.638 rows=381772 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{15,32,12,13,24,25,14}'::text[])) Buffers: shared hit=13 read=1050 -> HashAggregate (cost=641795.11..641795.16 rows=3 width=20) (actual time=9836.919..9836.921 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=203918 read=123194 dirtied=128 written=6167 -> Hash Anti Join (cost=597812.26..641354.34 rows=58770 width=20) (actual time=9509.496..9817.242 rows=63296 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=203918 read=123194 dirtied=128 written=6167 -> Bitmap Heap Scan on customer (cost=2909.93..41862.72 rows=139992 width=24) (actual time=701.549..875.223 rows=190519 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{15,32,12,13,24,25,14}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35181 Buffers: shared hit=35207 read=36861 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2874.93 rows=139992 width=0) (actual time=693.952..693.952 rows=190519 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{15,32,12,13,24,25,14}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=26 read=36861 -> Hash (cost=406090.48..406090.48 rows=15104948 width=4) (actual time=8712.164..8712.164 rows=15000000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658416kB Buffers: shared hit=168708 read=86333 dirtied=128 written=6167 -> Seq Scan on orders (cost=0.00..406090.48 rows=15104948 width=4) (actual time=0.043..4363.474 rows=15000000 loops=1) Buffers: shared hit=168708 read=86333 dirtied=128 written=6167 Planning time: 2.557 ms Execution time: 9859.185 ms (34 rows) COMMIT; COMMIT