BEGIN; BEGIN EXPLAIN (ANALYZE, BUFFERS) select s_name, count(*) as numwait from supplier, lineitem l1, orders, nation where s_suppkey = l1.l_suppkey and o_orderkey = l1.l_orderkey and o_orderstatus = 'F' and l1.l_receiptdate > l1.l_commitdate and exists ( select * from lineitem l2 where l2.l_orderkey = l1.l_orderkey and l2.l_suppkey <> l1.l_suppkey ) and not exists ( select * from lineitem l3 where l3.l_orderkey = l1.l_orderkey and l3.l_suppkey <> l1.l_suppkey and l3.l_receiptdate > l3.l_commitdate ) and s_nationkey = n_nationkey and n_name = 'PERU' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=2843301.51..2843301.51 rows=1 width=26) (actual time=42294.573..42294.590 rows=100 loops=1) Buffers: shared hit=13624246 read=720044 -> Sort (cost=2843301.51..2843301.51 rows=1 width=26) (actual time=42294.571..42294.577 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=13624246 read=720044 -> HashAggregate (cost=2843301.49..2843301.50 rows=1 width=26) (actual time=42292.349..42293.436 rows=3991 loops=1) Group Key: supplier.s_name Buffers: shared hit=13624238 read=720044 -> Nested Loop Anti Join (cost=2406.88..2843301.48 rows=1 width=26) (actual time=39.776..42231.269 rows=39320 loops=1) Buffers: shared hit=13624238 read=720044 -> Nested Loop Semi Join (cost=2406.31..2843299.70 rows=1 width=34) (actual time=39.767..39358.119 rows=705016 loops=1) Join Filter: (orders.o_orderkey = l2.l_orderkey) Buffers: shared hit=10086832 read=717372 -> Nested Loop (cost=2405.75..2323251.22 rows=389239 width=38) (actual time=39.751..34212.047 rows=731722 loops=1) Buffers: shared hit=6586053 read=554090 -> Hash Join (cost=2405.31..1902499.01 rows=799298 width=34) (actual time=39.726..26317.976 rows=1515797 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=537658 read=531000 -> Seq Scan on lineitem l1 (cost=0.00..1817043.65 rows=20015217 width=8) (actual time=0.005..20386.476 rows=37967116 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078553 Buffers: shared hit=535475 read=530998 -> Hash (cost=2355.31..2355.31 rows=4000 width=30) (actual time=39.618..39.618 rows=3991 loops=1) Buckets: 4096 Batches: 1 Memory Usage: 282kB Buffers: shared hit=2180 read=2 -> Nested Loop (cost=43.00..2355.31 rows=4000 width=30) (actual time=0.289..38.521 rows=3991 loops=1) Buffers: shared hit=2180 read=2 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.006..0.011 rows=1 loops=1) Filter: (n_name = 'PERU'::bpchar) Rows Removed by Filter: 24 Buffers: shared hit=1 -> Bitmap Heap Scan on supplier (cost=43.00..2314.00 rows=4000 width=34) (actual time=0.275..37.807 rows=3991 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Rows Removed by Index Recheck: 96009 Heap Blocks: lossy=2179 Buffers: shared hit=2179 read=2 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_brin_idx (cost=0.00..42.00 rows=4000 width=0) (actual time=0.263..0.263 rows=23040 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=2 -> Index Scan using pk_orders on orders (cost=0.43..0.52 rows=1 width=4) (actual time=0.004..0.005 rows=0 loops=1515797) Index Cond: (o_orderkey = l1.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=6048395 read=23090 -> Index Scan using pk_lineitem on lineitem l2 (cost=0.56..1.15 rows=15 width=8) (actual time=0.006..0.006 rows=1 loops=731722) Index Cond: (l_orderkey = l1.l_orderkey) Filter: (l_suppkey <> l1.l_suppkey) Rows Removed by Filter: 0 Buffers: shared hit=3500779 read=163282 -> Index Scan using pk_lineitem on lineitem l3 (cost=0.56..1.15 rows=5 width=8) (actual time=0.004..0.004 rows=1 loops=705016) Index Cond: (l_orderkey = l1.l_orderkey) Filter: ((l_receiptdate > l_commitdate) AND (l_suppkey <> l1.l_suppkey)) Rows Removed by Filter: 1 Buffers: shared hit=3537406 read=2672 Planning time: 9.874 ms Execution time: 42294.879 ms (57 rows) COMMIT; COMMIT