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 = 'INDIA' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4866410.24..4866410.25 rows=1 width=26) (actual time=160453.046..160453.062 rows=100 loops=1) Buffers: shared hit=10083797 read=1688634 dirtied=247 written=54252, temp read=161870 written=161864 -> Sort (cost=4866410.24..4866410.25 rows=1 width=26) (actual time=160453.043..160453.048 rows=100 loops=1) Sort Key: (count(*)), supplier.s_name Sort Method: top-N heapsort Memory: 32kB Buffers: shared hit=10083797 read=1688634 dirtied=247 written=54252, temp read=161870 written=161864 -> HashAggregate (cost=4866410.22..4866410.23 rows=1 width=26) (actual time=160448.611..160450.360 rows=4079 loops=1) Buffers: shared hit=10083789 read=1688634 dirtied=247 written=54252, temp read=161870 written=161864 -> Nested Loop Anti Join (cost=2688918.71..4866410.22 rows=1 width=26) (actual time=57023.597..160301.936 rows=40456 loops=1) Buffers: shared hit=10083789 read=1688634 dirtied=247 written=54252, temp read=161870 written=161864 -> Nested Loop (cost=2688918.71..4866395.00 rows=1 width=34) (actual time=57021.915..145363.602 rows=721382 loops=1) Join Filter: (l1.l_orderkey = orders.o_orderkey) Buffers: shared hit=6914895 read=1235298 dirtied=158 written=24203, temp read=161870 written=161864 -> Hash Semi Join (cost=2688918.71..4866393.01 rows=1 width=38) (actual time=57020.433..128309.903 rows=1494853 loops=1) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) Rows Removed by Join Filter: 388642 Buffers: shared hit=1096482 read=1066023 written=4, temp read=161870 written=161864 -> Hash Join (cost=2441.58..1927212.87 rows=810999 width=34) (actual time=24.326..54624.555 rows=1550526 loops=1) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) Buffers: shared hit=557598 read=524588 written=4 -> Seq Scan on lineitem l1 (cost=0.00..1840630.19 rows=20274965 width=8) (actual time=0.029..35134.065 rows=37966096 loops=1) Filter: (l_receiptdate > l_commitdate) Rows Removed by Filter: 22078937 Buffers: shared hit=555745 read=524574 written=4 -> Hash (cost=2391.58..2391.58 rows=4000 width=30) (actual time=24.244..24.244 rows=4079 loops=1) Buckets: 1024 Batches: 1 Memory Usage: 255kB Buffers: shared hit=1850 read=14 -> Nested Loop (cost=79.26..2391.58 rows=4000 width=30) (actual time=1.767..21.965 rows=4079 loops=1) Buffers: shared hit=1850 read=14 -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) (actual time=0.010..0.018 rows=1 loops=1) Filter: (n_name = 'INDIA'::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.750..20.521 rows=4079 loops=1) Recheck Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1849 read=14 -> Bitmap Index Scan on supplier_s_nationkey_s_suppkey_idx (cost=0.00..78.26 rows=4000 width=0) (actual time=1.367..1.367 rows=4079 loops=1) Index Cond: (s_nationkey = nation.n_nationkey) Buffers: shared hit=1 read=13 -> Hash (cost=1688567.95..1688567.95 rows=60824895 width=8) (actual time=56979.705..56979.705 rows=60045033 loops=1) Buckets: 4194304 Batches: 4 Memory Usage: 586575kB Buffers: shared hit=538884 read=541435, temp written=153905 -> Seq Scan on lineitem l2 (cost=0.00..1688567.95 rows=60824895 width=8) (actual time=0.060..27057.354 rows=60045033 loops=1) Buffers: shared hit=538884 read=541435 -> Index Scan using orders_o_orderkey_o_orderdate_idx on orders (cost=0.00..1.98 rows=1 width=4) (actual time=0.010..0.011 rows=0 loops=1494853) Index Cond: (o_orderkey = l2.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) Rows Removed by Filter: 1 Buffers: shared hit=5818413 read=169275 dirtied=158 written=24199 -> Index Scan using pk_lineitem on lineitem l3 (cost=0.00..7.58 rows=5 width=8) (actual time=0.020..0.020 rows=1 loops=721382) 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=3168894 read=453336 dirtied=89 written=30049 Total runtime: 160454.396 ms (56 rows) COMMIT; COMMIT