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 = 'MOROCCO' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4804104.55..4804104.56 rows=1 width=26) (actual time=156351.455..156351.480 rows=100 loops=1) Buffers: shared hit=10196250 read=1315757 dirtied=64 written=19560, temp read=161512 written=161506 -> Sort (cost=4804104.55..4804104.56 rows=1 width=26) (actual time=156351.451..156351.463 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=10196250 read=1315757 dirtied=64 written=19560, temp read=161512 written=161506 -> HashAggregate (cost=4804104.53..4804104.54 rows=1 width=26) (actual time=156344.172..156346.560 rows=3989 loops=1) Buffers: shared hit=10196242 read=1315757 dirtied=64 written=19560, temp read=161512 written=161506 -> Nested Loop Anti Join (cost=2654506.96..4804104.53 rows=1 width=26) (actual time=51172.885..156188.566 rows=39724 loops=1) Buffers: shared hit=10196242 read=1315757 dirtied=64 written=19560, temp read=161512 written=161506 -> Nested Loop (cost=2654506.96..4804089.63 rows=1 width=34) (actual time=51164.189..136714.025 rows=703613 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=7138720 read=840283 dirtied=64 written=3662, temp read=161512 written=161506 -> Hash Semi Join (cost=2654506.96..4804087.67 rows=1 width=38) (actual time=51164.143..116724.635 rows=1459073 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 378205 Buffers: shared hit=1471614 read=663207 dirtied=16 written=3, temp read=161512 written=161506 -> Hash Join (cost=2441.58..1902555.45 rows=800614 width=34) (actual time=63.936..42490.158 rows=1513095 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=597594 read=470752 -> Seq Scan on lineitem l1 (cost=0.00..1817050.21 rows=20015339 width=8) (actual time=0.062..29332.321 rows=37928797 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22056532 Buffers: shared hit=596070 read=470405 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=63.800..63.800 rows=3990 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 250kB Buffers: shared hit=1521 read=347 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.644..61.481 rows=3990 loops=1) Buffers: shared hit=1521 read=347 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.019..0.031 rows=1 loops=1) Filter: (n_name = 'MOROCCO'::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=1.611..59.979 rows=3990 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1520 read=347 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.268..1.268 rows=3990 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=2 read=12 -> Hash (cost=1666935.17..1666935.17 rows=60046017 width=8) (actual time=51073.908..51073.908 rows=59985329 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586039kB Buffers: shared hit=874020 read=192455 dirtied=16 written=3, temp written=153747 -> Seq Scan on lineitem l2 (cost=0.00..1666935.17 rows=60046017 width=8) (actual time=0.020..25443.093 rows=59985329 loops=1) Buffers: shared hit=874020 read=192455 dirtied=16 written=3 -> Index Scan using pk_orders on orders (cost=0.00..1.94 rows=1 width=4) (actual time=0.012..0.013 rows=0 loops=1459073) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5667106 read=177076 dirtied=48 written=3659 -> Index Scan using pk_lineitem on lineitem l3 (cost=0.00..7.42 rows=5 width=8) (actual time=0.027..0.027 rows=1 loops=703613) 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=3057522 read=475474 written=15898 Total runtime: 156352.766 ms (56 rows) COMMIT; COMMIT