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 ('31', '30', '25', '19', '24', '13', '17') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('31', '30', '25', '19', '24', '13', '17') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=698375.75..698375.76 rows=3 width=20) (actual time=13646.613..13646.614 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=268081 read=60566 dirtied=506 InitPlan 1 (returns $0) -> Aggregate (cost=52950.90..52950.91 rows=1 width=4) (actual time=1193.705..1193.705 rows=1 loops=1) Buffers: shared hit=12 read=36330 -> Bitmap Heap Scan on customer (cost=6674.67..51994.53 rows=382547 width=4) (actual time=207.139..1085.486 rows=381508 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{31,30,25,19,24,13,17}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=12 read=36330 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6579.04 rows=382547 width=0) (actual time=185.177..185.177 rows=381508 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{31,30,25,19,24,13,17}'::text[])) Buffers: shared hit=9 read=1055 -> HashAggregate (cost=645424.77..645424.82 rows=3 width=20) (actual time=13646.540..13646.542 rows=7 loops=1) Buffers: shared hit=268079 read=60566 dirtied=506 -> Hash Anti Join (cost=601386.23..644977.68 rows=59612 width=20) (actual time=13112.282..13611.915 rows=63684 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=268079 read=60566 dirtied=506 -> Bitmap Heap Scan on customer (cost=2940.73..41903.52 rows=140373 width=24) (actual time=1302.454..1500.723 rows=190508 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{31,30,25,19,24,13,17}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35216 read=36866 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2905.64 rows=140373 width=0) (actual time=1288.545..1288.545 rows=190508 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{31,30,25,19,24,13,17}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=22 read=36866 -> Hash (cost=408509.11..408509.11 rows=15194911 width=4) (actual time=11798.807..11798.807 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=232860 read=23700 dirtied=506 -> Seq Scan on orders (cost=0.00..408509.11 rows=15194911 width=4) (actual time=0.025..5912.895 rows=15015000 loops=1) Buffers: shared hit=232860 read=23700 dirtied=506 Total runtime: 13706.561 ms (30 rows) COMMIT; COMMIT