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 ('26', '31', '10', '24', '32', '16', '15') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('26', '31', '10', '24', '32', '16', '15') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=693995.61..693995.61 rows=3 width=20) (actual time=23385.409..23385.411 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=188947 read=138149 written=38 InitPlan 1 (returns $0) -> Aggregate (cost=52815.86..52815.87 rows=1 width=4) (actual time=2156.636..2156.636 rows=1 loops=1) Buffers: shared hit=13 read=36330 -> Bitmap Heap Scan on customer (cost=6611.31..51865.72 rows=380054 width=4) (actual time=259.569..2028.082 rows=381973 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{26,31,10,24,32,16,15}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=13 read=36330 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6516.29 rows=380054 width=0) (actual time=244.186..244.186 rows=381973 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{26,31,10,24,32,16,15}'::text[])) Buffers: shared hit=10 read=1055 -> HashAggregate (cost=641179.67..641179.71 rows=3 width=20) (actual time=23385.339..23385.343 rows=7 loops=1) Buffers: shared hit=188945 read=138149 written=38 -> Hash Anti Join (cost=597803.65..640741.92 rows=58366 width=20) (actual time=22205.654..23328.854 rows=63232 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=188945 read=138149 written=38 -> Bitmap Heap Scan on customer (cost=2901.32..41834.82 rows=139257 width=24) (actual time=2312.894..2966.710 rows=191049 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{26,31,10,24,32,16,15}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=31880 read=40170 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2866.51 rows=139257 width=0) (actual time=2300.931..2300.931 rows=191049 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{26,31,10,24,32,16,15}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=24 read=36867 -> Hash (cost=406090.48..406090.48 rows=15104948 width=4) (actual time=19881.113..19881.113 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=157062 read=97979 written=38 -> Seq Scan on orders (cost=0.00..406090.48 rows=15104948 width=4) (actual time=0.174..10480.371 rows=15015000 loops=1) Buffers: shared hit=157062 read=97979 written=38 Total runtime: 23517.038 ms (30 rows) COMMIT; COMMIT