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 ('30', '31', '29', '15', '32', '25', '22') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('30', '31', '29', '15', '32', '25', '22') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=692889.61..692889.61 rows=3 width=20) (actual time=9708.128..9708.129 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=313757 read=12830 InitPlan 1 (returns $0) -> Aggregate (cost=52898.10..52898.11 rows=1 width=4) (actual time=482.932..482.932 rows=1 loops=1) Buffers: shared hit=35287 read=1054 -> Bitmap Heap Scan on customer customer_1 (cost=6645.42..51943.78 rows=381728 width=4) (actual time=82.289..402.347 rows=381913 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{30,31,29,15,32,25,22}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35277 Buffers: shared hit=35287 read=1054 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6549.99 rows=381728 width=0) (actual time=73.969..73.969 rows=381913 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{30,31,29,15,32,25,22}'::text[])) Buffers: shared hit=10 read=1054 -> HashAggregate (cost=639991.43..639991.47 rows=3 width=20) (actual time=9708.026..9708.027 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=313754 read=12830 -> Hash Anti Join (cost=596629.06..639545.21 rows=59495 width=20) (actual time=9430.099..9690.437 rows=63841 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=313754 read=12830 -> Bitmap Heap Scan on customer (cost=2909.36..41860.78 rows=139940 width=24) (actual time=533.515..685.437 rows=190982 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{30,31,29,15,32,25,22}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35157 Buffers: shared hit=70454 read=1593 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2874.37 rows=139940 width=0) (actual time=526.226..526.226 rows=190982 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{30,31,29,15,32,25,22}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35297 read=1593 -> Hash (cost=405283.20..405283.20 rows=15074920 width=4) (actual time=8786.903..8786.903 rows=15000000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658416kB Buffers: shared hit=243297 read=11237 -> Seq Scan on orders (cost=0.00..405283.20 rows=15074920 width=4) (actual time=0.019..4041.132 rows=15000000 loops=1) Buffers: shared hit=243297 read=11237 Planning time: 2.461 ms Execution time: 9712.218 ms (34 rows) COMMIT; COMMIT