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 ('18', '26', '12', '15', '20', '33', '24') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('18', '26', '12', '15', '20', '33', '24') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=698494.29..698494.29 rows=3 width=20) (actual time=19101.661..19101.661 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=250363 read=78274 dirtied=54 written=6106 InitPlan 1 (returns $0) -> Aggregate (cost=53018.10..53018.11 rows=1 width=4) (actual time=1446.697..1446.697 rows=1 loops=1) Buffers: shared hit=9 read=36336 written=3573 -> Bitmap Heap Scan on customer (cost=6688.94..52057.13 rows=384388 width=4) (actual time=164.325..1318.061 rows=382198 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,26,12,15,20,33,24}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=9 read=36336 written=3573 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6592.84 rows=384388 width=0) (actual time=151.147..151.147 rows=382198 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{18,26,12,15,20,33,24}'::text[])) Buffers: shared hit=6 read=1061 written=97 -> HashAggregate (cost=645476.11..645476.15 rows=3 width=20) (actual time=19101.570..19101.572 rows=7 loops=1) Buffers: shared hit=250361 read=78274 dirtied=54 written=6106 -> Hash Anti Join (cost=601392.25..645030.85 rows=59368 width=20) (actual time=18304.310..19056.849 rows=63618 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=250361 read=78274 dirtied=54 written=6106 -> Bitmap Heap Scan on customer (cost=2946.76..41924.90 rows=140958 width=24) (actual time=1608.138..1959.484 rows=190978 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,26,12,15,20,33,24}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=34195 read=37877 written=3625 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2911.52 rows=140958 width=0) (actual time=1587.139..1587.139 rows=190978 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{18,26,12,15,20,33,24}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=18 read=36875 written=3625 -> Hash (cost=408509.11..408509.11 rows=15194911 width=4) (actual time=16681.672..16681.672 rows=15000000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527344kB Buffers: shared hit=216163 read=40397 dirtied=54 written=2481 -> Seq Scan on orders (cost=0.00..408509.11 rows=15194911 width=4) (actual time=0.059..7609.096 rows=15000000 loops=1) Buffers: shared hit=216163 read=40397 dirtied=54 written=2481 Total runtime: 19251.805 ms (30 rows) COMMIT; COMMIT