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 ('33', '24', '10', '28', '19', '27', '14') and c_acctbal > ( select avg(c_acctbal) from customer where c_acctbal > 0.00 and substr(c_phone, 1, 2) in ('33', '24', '10', '28', '19', '27', '14') ) and not exists ( select * from orders where o_custkey = c_custkey ) ) as vip group by cntrycode order by cntrycode; QUERY PLAN ---------------------------------------------------------------------------------------------------------------------------------------------------------------------- Sort (cost=693952.57..693952.58 rows=3 width=20) (actual time=24349.713..24349.715 rows=7 loops=1) Sort Key: (substr((public.customer.c_phone)::text, 1, 2)) Sort Method: quicksort Memory: 25kB Buffers: shared hit=313817 read=12778 written=6 InitPlan 1 (returns $0) -> Aggregate (cost=52844.78..52844.79 rows=1 width=4) (actual time=1437.466..1437.467 rows=1 loops=1) Buffers: shared hit=35059 read=1283 written=1 -> Bitmap Heap Scan on customer (cost=6633.21..51894.03 rows=380298 width=4) (actual time=247.515..1300.821 rows=381479 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{33,24,10,28,19,27,14}'::text[])) AND (c_acctbal > 0::double precision)) Buffers: shared hit=35059 read=1283 written=1 -> Bitmap Index Scan on customer_c_phone_idx_c_acctbal (cost=0.00..6538.13 rows=380298 width=0) (actual time=235.238..235.238 rows=381479 loops=1) Index Cond: (substr((c_phone)::text, 1, 2) = ANY ('{33,24,10,28,19,27,14}'::text[])) Buffers: shared hit=7 read=1057 written=1 -> HashAggregate (cost=641107.72..641107.76 rows=3 width=20) (actual time=24349.632..24349.634 rows=7 loops=1) Buffers: shared hit=313815 read=12778 written=6 -> Hash Anti Join (cost=596622.60..640665.37 rows=58980 width=20) (actual time=22952.760..24289.235 rows=63417 loops=1) Hash Cond: (public.customer.c_custkey = orders.o_custkey) Buffers: shared hit=313815 read=12778 written=6 -> Bitmap Heap Scan on customer (cost=2902.90..41840.41 rows=139410 width=24) (actual time=1541.907..2431.748 rows=190366 loops=1) Recheck Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{33,24,10,28,19,27,14}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=70239 read=1817 written=1 -> Bitmap Index Scan on customer_c_phone_idx (cost=0.00..2868.05 rows=139410 width=0) (actual time=1530.879..1530.879 rows=190366 loops=1) Index Cond: ((substr((c_phone)::text, 1, 2) = ANY ('{33,24,10,28,19,27,14}'::text[])) AND (c_acctbal > $0)) Buffers: shared hit=35069 read=1817 written=1 -> Hash (cost=405283.20..405283.20 rows=15074920 width=4) (actual time=21400.003..21400.003 rows=15015000 loops=1) Buckets: 2097152 Batches: 1 Memory Usage: 527872kB Buffers: shared hit=243573 read=10961 written=5 -> Seq Scan on orders (cost=0.00..405283.20 rows=15074920 width=4) (actual time=0.020..12504.456 rows=15015000 loops=1) Buffers: shared hit=243573 read=10961 written=5 Total runtime: 24511.647 ms (30 rows) COMMIT; COMMIT