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', '28', '19', '26', '15', '22', '32') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('30', '28', '19', '26', '15', '22', '32') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=693461.18..693461.18 rows=3 width=20) (actual time=19280.488..19280.488 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=284784 read=42067 written=9 InitPlan 1 (returns $0) -> Aggregate (cost=52858.49..52858.50 rows=1 width=4) (actual time=1158.956..1158.956 rows=1 loops=1) Buffers: shared hit=35284 read=1061 -> Bitmap Heap Scan on customer (cost=6636.13..51906.80 rows=380673 width=4) (actual time=207.374..1019.153 rows=382227 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{30,28,19,26,15,22,32}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=35284 read=1061 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6540.97 rows=380673 width=0) (actual time=192.828..192.828 rows=382227 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{30,28,19,26,15,22,32}'::text[])) Buffers: shared hit=6 read=1061 -> HashAggregate (cost=640602.61..640602.66 rows=3 width=20) (actual time=19280.411..19280.414 rows=7 loops=1) Buffers: shared hit=284782 read=42067 written=9 -> Hash Anti Join (cost=597213.46..640164.15 rows=58461 width=20) (actual time=17819.821..19221.613 rows=63551 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=284782 read=42067 written=9 -> Bitmap Heap Scan on customer (cost=2903.62..41843.05 rows=139483 width=24) (actual time=1299.789..2214.136 rows=191134 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{30,28,19,26,15,22,32}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=52001 read=20058 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2868.75 rows=139483 width=0) (actual time=1284.142..1284.142 rows=191134 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{30,28,19,26,15,22,32}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35293 read=1599 -> Hash (cost=405686.04..405686.04 rows=15089904 width=4) (actual time=16507.716..16507.716 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=232778 read=22009 written=9 -> Seq Scan on orders (cost=0.00..405686.04 rows=15089904 width=4) (actual time=0.100..8136.931 rows=15015000 loops=1) Buffers: shared hit=232778 read=22009 written=9 Total runtime: 19405.281 ms (30 rows) COMMIT; COMMIT