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 = 'ETHIOPIA' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4832772.74..4832772.75 rows=1 width=26) (actual time=128137.320..128137.339 rows=100 loops=1) Buffers: shared hit=9626885 read=1803546 dirtied=1065 written=20733, temp read=161590 written=161584 -> Sort (cost=4832772.74..4832772.75 rows=1 width=26) (actual time=128137.310..128137.318 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=9626885 read=1803546 dirtied=1065 written=20733, temp read=161590 written=161584 -> HashAggregate (cost=4832772.72..4832772.73 rows=1 width=26) (actual time=128132.635..128134.396 rows=3945 loops=1) Buffers: shared hit=9626877 read=1803546 dirtied=1065 written=20733, temp read=161590 written=161584 -> Nested Loop Anti Join (cost=2670352.70..4832772.72 rows=1 width=26) (actual time=45864.868..127997.883 rows=38998 loops=1) Buffers: shared hit=9626877 read=1803546 dirtied=1065 written=20733, temp read=161590 written=161584 -> Nested Loop (cost=2670352.70..4832757.67 rows=1 width=34) (actual time=45861.496..113064.938 rows=697366 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6756979 read=1170392 dirtied=1065 written=208, temp read=161590 written=161584 -> Hash Semi Join (cost=2670352.70..4832755.70 rows=1 width=38) (actual time=45861.035..99728.154 rows=1442982 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 374825 Buffers: shared hit=996564 read=1151008 dirtied=1064 written=3, temp read=161590 written=161584 -> Hash Join (cost=2441.58..1913904.00 rows=804524 width=34) (actual time=17.879..40364.014 rows=1496367 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=395119 read=679597 written=3 -> Seq Scan on lineitem l1 (cost=0.00..1827911.62 rows=20134817 width=8) (actual time=0.035..26670.348 rows=37966100 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22079210 Buffers: shared hit=393273 read=679583 written=3 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=17.798..17.798 rows=3945 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 247kB Buffers: shared hit=1843 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.340..16.464 rows=3945 loops=1) Buffers: shared hit=1843 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.007..0.014 rows=1 loops=1) Filter: (n_name = 'ETHIOPIA'::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.327..15.554 rows=3945 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.023..1.023 rows=3945 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared read=14 -> Hash (cost=1676900.50..1676900.50 rows=60404450 width=8) (actual time=45820.036..45820.036 rows=60045310 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586609kB Buffers: shared hit=601445 read=471411 dirtied=1064, temp written=153904 -> Seq Scan on lineitem l2 (cost=0.00..1676900.50 rows=60404450 width=8) (actual time=0.030..22073.434 rows=60045310 loops=1) Buffers: shared hit=601445 read=471411 dirtied=1064 -> Index Scan using pk_orders on orders (cost=0.00..1.96 rows=1 width=4) (actual time=0.008..0.008 rows=0 loops=1442982) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5760415 read=19384 dirtied=1 written=205 -> Index Scan using lineitem_l_orderkey_idx on lineitem l3 (cost=0.00..7.50 rows=5 width=8) (actual time=0.021..0.021 rows=1 loops=697366) 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=2869898 read=633154 written=20525 Total runtime: 128138.742 ms (56 rows) COMMIT; COMMIT