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 ('27', '20', '32', '34', '10', '30', '29') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('27', '20', '32', '34', '10', '30', '29') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=697609.81..697609.82 rows=3 width=20) (actual time=12067.561..12067.561 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=204762 read=123611 written=5385 InitPlan 1 (returns $0) -> Aggregate (cost=52873.06..52873.07 rows=1 width=4) (actual time=803.954..803.955 rows=1 loops=1) Buffers: shared hit=9 read=36330 written=1663 -> Bitmap Heap Scan on customer customer_1 (cost=6640.10..51920.45 rows=381042 width=4) (actual time=83.422..735.212 rows=381480 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{27,20,32,34,10,30,29}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35277 Buffers: shared hit=9 read=36330 written=1663 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6544.84 rows=381042 width=0) (actual time=75.802..75.802 rows=381480 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{27,20,32,34,10,30,29}'::text[])) Buffers: shared hit=7 read=1055 written=2 -> HashAggregate (cost=644736.67..644736.71 rows=3 width=20) (actual time=12067.506..12067.508 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=204759 read=123611 written=5385 -> Hash Anti Join (cost=600763.57..644296.59 rows=58677 width=20) (actual time=11329.125..12043.283 rows=64047 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=204759 read=123611 written=5385 -> Bitmap Heap Scan on customer (cost=2908.21..41856.70 rows=139828 width=24) (actual time=859.304..1385.728 rows=190756 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{27,20,32,34,10,30,29}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35175 Buffers: shared hit=21 read=72039 written=1691 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2873.25 rows=139828 width=0) (actual time=851.471..851.471 rows=190756 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{27,20,32,34,10,30,29}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=19 read=36866 written=1691 -> Hash (cost=408106.27..408106.27 rows=15179927 width=4) (actual time=10364.610..10364.610 rows=15000000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658416kB Buffers: shared hit=204735 read=51572 written=3694 -> Seq Scan on orders (cost=0.00..408106.27 rows=15179927 width=4) (actual time=0.032..4527.809 rows=15000000 loops=1) Buffers: shared hit=204735 read=51572 written=3694 Planning time: 2.767 ms Execution time: 12083.285 ms (34 rows) COMMIT; COMMIT