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 ('34', '28', '22', '16', '15', '31', '19') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('34', '28', '22', '16', '15', '31', '19') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=693095.77..693095.78 rows=3 width=20) (actual time=10277.513..10277.514 rows=7 loops=1) Sort Key: (substr((customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=141039 read=185564 dirtied=507 InitPlan 1 (returns $0) -> Aggregate (cost=53013.66..53013.67 rows=1 width=4) (actual time=435.068..435.068 rows=1 loops=1) Buffers: shared hit=34082 read=2263 -> Bitmap Heap Scan on customer customer_1 (cost=6688.84..52053.06 rows=384237 width=4) (actual time=74.995..364.089 rows=382510 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{34,28,22,16,15,31,19}'::text[])) AND (c_acctbal > 0::double precision)) Heap Blocks: exact=35278 Buffers: shared hit=34082 read=2263 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6592.78 rows=384237 width=0) (actual time=67.894..67.894 rows=382510 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{34,28,22,16,15,31,19}'::text[])) Buffers: shared hit=7 read=1060 -> HashAggregate (cost=640082.04..640082.08 rows=3 width=20) (actual time=10277.454..10277.457 rows=7 loops=1) Group Key: substr((customer.c_phone)::text, 1, 2) Buffers: shared hit=141036 read=185564 dirtied=507 -> Hash Anti Join (cost=596666.53..639632.88 rows=59887 width=20) (actual time=10008.675..10259.960 rows=63288 loops=1) Hash Cond: (customer.c_custkey = orders.o_custkey) Buffers: shared hit=141036 read=185564 dirtied=507 -> Bitmap Heap Scan on customer (cost=2946.83..41922.40 rows=140860 width=24) (actual time=486.408..632.072 rows=191244 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{34,28,22,16,15,31,19}'::text[])) AND (c_acctbal > $0)) Heap Blocks: exact=35171 Buffers: shared hit=69263 read=2800 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2911.61 rows=140860 width=0) (actual time=479.056..479.056 rows=191244 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{34,28,22,16,15,31,19}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=34092 read=2800 -> Hash (cost=405283.20..405283.20 rows=15074920 width=4) (actual time=9392.517..9392.517 rows=15000000 loops=1) Buckets: 16777216 Batches: 1 Memory Usage: 658416kB Buffers: shared hit=71770 read=182764 dirtied=507 -> Seq Scan on orders (cost=0.00..405283.20 rows=15074920 width=4) (actual time=0.028..4667.466 rows=15000000 loops=1) Buffers: shared hit=71770 read=182764 dirtied=507 Planning time: 2.406 ms Execution time: 10291.035 ms (34 rows) COMMIT; COMMIT