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=4866363.84..4866363.84 rows=1 width=26) (actual time=137194.303..137194.322 rows=100 loops=1) Buffers: shared hit=9590363 read=1968486 dirtied=1427 written=4687, temp read=161690 written=161684 -> Sort (cost=4866363.84..4866363.84 rows=1 width=26) (actual time=137194.300..137194.306 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9590363 read=1968486 dirtied=1427 written=4687, temp read=161690 written=161684 -> HashAggregate (cost=4866363.82..4866363.83 rows=1 width=26) (actual time=137189.734..137191.461 rows=3991 loops=1) Buffers: shared hit=9590355 read=1968486 dirtied=1427 written=4687, temp read=161690 written=161684 -> Nested Loop Anti Join (cost=2688908.86..4866363.81 rows=1 width=26) (actual time=45470.010..137050.820 rows=39351 loops=1) Buffers: shared hit=9590355 read=1968486 dirtied=1427 written=4687, temp read=161690 written=161684 -> Nested Loop (cost=2688908.86..4866348.60 rows=1 width=34) (actual time=45466.289..122267.592 rows=705030 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6584774 read=1432858 dirtied=1245 written=2038, temp read=161690 written=161684 -> Hash Semi Join (cost=2688908.86..4866346.60 rows=1 width=38) (actual time=45465.841..107424.700 rows=1461808 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 377661 Buffers: shared hit=793955 read=1368543 dirtied=1201 written=137, temp read=161690 written=161684 -> Hash Join (cost=2441.58..1927197.49 rows=809941 width=34) (actual time=34.730..49917.030 rows=1515809 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=402308 read=679871 written=137 -> Seq Scan on lineitem l1 (cost=0.00..1840625.83 rows=20274849 width=8) (actual time=0.043..32660.950 rows=37966096 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078937 Buffers: shared hit=400462 read=679857 written=137 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=34.589..34.589 rows=3991 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 250kB Buffers: shared hit=1843 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=2.139..31.988 rows=3991 loops=1) Buffers: shared hit=1843 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.014..0.020 rows=1 loops=1) Filter: (n_name = 'PERU'::bpchar) Rows Removed by Filter: 24 Buffers: shared hit=1 -> Bitmap Heap Scan on supplier (cost=79.26..2350.26 rows=4000 width=34) (actual time=2.118..30.244 rows=3991 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1842 read=14 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.693..1.693 rows=3991 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1688564.46..1688564.46 rows=60824546 width=8) (actual time=45374.756..45374.756 rows=60045033 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586575kB Buffers: shared hit=391647 read=688672 dirtied=1201, temp written=153905 -> Seq Scan on lineitem l2 (cost=0.00..1688564.46 rows=60824546 width=8) (actual time=0.060..23721.335 rows=60045033 loops=1) Buffers: shared hit=391647 read=688672 dirtied=1201 -> Index Scan using orders_o_orderkey_o_orderdate_idx on orders (cost=0.00..1.98 rows=1 width=4) (actual time=0.009..0.009 rows=0 loops=1461808) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5790819 read=64315 dirtied=44 written=1901 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.58 rows=5 width=8) (actual time=0.020..0.020 rows=1 loops=705030) 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=3005581 read=535628 dirtied=182 written=2649 Total runtime: 137195.735 ms (56 rows) COMMIT; COMMIT