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 ('12', '21', '13', '23', '25', '17', '11') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('12', '21', '13', '23', '25', '17', '11') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=694109.85..694109.85 rows=3 width=20) (actual time=11620.596..11620.597 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=244432 read=82428 dirtied=20 written=29 InitPlan 1 (returns $0) -> Aggregate (cost=52899.41..52899.42 rows=1 width=4) (actual time=536.803..536.804 rows=1 loops=1) Buffers: shared hit=35287 read=1053 -> Bitmap Heap Scan on customer customer_1 (cost=6645.69..51944.99 rows=381764 width=4) (actual time=93.139..456.877 rows=381595 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{12,21,13,23,25,17,11}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=35287 read=1053 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6550.25 rows=381764 width=0) (actual time=84.474..84.474 rows=381595 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{12,21,13,23,25,17,11}'::text[])) Buffers: shared hit=9 read=1053 -> HashAggregate (cost=641210.36..641210.41 rows=3 width=20) (actual time=11620.542..11620.543 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=244429 read=82428 dirtied=20 written=29 -> Hash Anti Join (cost=597220.78..640769.45 rows=58788 width=20) (actual time=10889.242..11597.460 rows=63328 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=244429 read=82428 dirtied=20 written=29 -> Bitmap Heap Scan on customer (cost=2910.94..41866.38 rows=140093 width=24) (actual time=599.541..1172.881 rows=190443 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{12,21,13,23,25,17,11}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35181 Buffers: shared hit=35658 read=36409 written=8 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2875.91 rows=140093 width=0) (actual time=590.715..590.715 rows=190443 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{12,21,13,23,25,17,11}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35296 read=1590 -> Hash (cost=405686.04..405686.04 rows=15089904 width=4) (actual time=10171.746..10171.746 rows=15015000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658944kB Buffers: shared hit=208768 read=46019 dirtied=20 written=21 -> Seq Scan on orders (cost=0.00..405686.04 rows=15089904 width=4) (actual time=0.030..4723.874 rows=15015000 loops=1) Buffers: shared hit=208768 read=46019 dirtied=20 written=21 Planning time: 2.452 ms Execution time: 11625.370 ms (34 rows) COMMIT; COMMIT