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 = 'GERMANY' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4804053.26..4804053.26 rows=1 width=26) (actual time=351236.143..351236.161 rows=100 loops=1) Buffers: shared hit=10687639 read=962343 dirtied=24 written=2400, temp read=161624 written=161618 -> Sort (cost=4804053.26..4804053.26 rows=1 width=26) (actual time=351236.140..351236.146 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=10687639 read=962343 dirtied=24 written=2400, temp read=161624 written=161618 -> HashAggregate (cost=4804053.24..4804053.25 rows=1 width=26) (actual time=351230.998..351233.133 rows=4049 loops=1) Buffers: shared hit=10687631 read=962343 dirtied=24 written=2400, temp read=161624 written=161618 -> Nested Loop Anti Join (cost=2654478.55..4804053.23 rows=1 width=26) (actual time=80125.991..350981.255 rows=40107 loops=1) Buffers: shared hit=10687631 read=962343 dirtied=24 written=2400, temp read=161624 written=161618 -> Nested Loop (cost=2654478.55..4804038.33 rows=1 width=34) (actual time=80115.050..256675.298 rows=714640 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=7534509 read=526962 dirtied=24 written=369, temp read=161624 written=161618 -> Hash Semi Join (cost=2654478.55..4804036.38 rows=1 width=38) (actual time=80114.983..177517.736 rows=1479615 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 384222 Buffers: shared hit=1691111 read=443688 dirtied=24 written=134, temp read=161624 written=161618 -> Hash Join (cost=2441.58..1902539.02 rows=800463 width=34) (actual time=135.937..78963.291 rows=1534314 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=877380 read=190944 written=115 -> Seq Scan on lineitem l1 (cost=0.00..1817036.65 rows=20014977 width=8) (actual time=0.029..55947.463 rows=37928797 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22056532 Buffers: shared hit=876131 read=190344 written=115 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=135.842..135.842 rows=4049 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 254kB Buffers: shared hit=1246 read=600 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=4.065..133.283 rows=4049 loops=1) Buffers: shared hit=1246 read=600 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.007..0.020 rows=1 loops=1) Filter: (n_name = 'GERMANY'::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=4.052..131.754 rows=4049 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1245 read=600 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=3.425..3.425 rows=4049 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=2 read=12 -> Hash (cost=1666924.32..1666924.32 rows=60044932 width=8) (actual time=79960.075..79960.075 rows=59985329 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586039kB Buffers: shared hit=813731 read=252744 dirtied=24 written=19, temp written=153747 -> Seq Scan on lineitem l2 (cost=0.00..1666924.32 rows=60044932 width=8) (actual time=0.026..52060.311 rows=59985329 loops=1) Buffers: shared hit=813731 read=252744 dirtied=24 written=19 -> Index Scan using pk_orders on orders (cost=0.00..1.94 rows=1 width=4) (actual time=0.052..0.052 rows=0 loops=1479615) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5843398 read=83274 written=235 -> Index Scan using pk_lineitem on lineitem l3 (cost=0.00..7.42 rows=5 width=8) (actual time=0.130..0.130 rows=1 loops=714640) 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=3153122 read=435381 written=2031 Total runtime: 351237.695 ms (56 rows) COMMIT; COMMIT