EXPLAIN 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 = 'FRANCE' group by s_name order by numwait desc, s_name LIMIT 100; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------- Limit (cost=4798923.80..4798923.80 rows=1 width=26) -> Sort (cost=4798923.80..4798923.80 rows=1 width=26) Sort Key: (count(*)), supplier.s_name -> HashAggregate (cost=4798923.78..4798923.79 rows=1 width=26) Group Key: supplier.s_name -> Nested Loop Anti Join (cost=2651855.37..4798923.77 rows=1 width=26) -> Nested Loop (cost=2651854.80..4798889.36 rows=1 width=34) -> Hash Semi Join (cost=2651854.37..4798882.09 rows=1 width=38) Hash Cond: (l1.l_orderkey = l2.l_orderkey) Join Filter: (l2.l_suppkey <> l1.l_suppkey) -> Hash Join (cost=2442.61..1900515.21 rows=785708 width=34) Hash Cond: (l1.l_suppkey = supplier.s_suppkey) -> Seq Scan on lineitem l1 (cost=0.00..1815233.20 rows=19995285 width=8) Filter: (l_receiptdate > l_commitdate) -> Hash (cost=2392.61..2392.61 rows=4000 width=30) -> Nested Loop (cost=79.29..2392.61 rows=4000 width=30) -> Seq Scan on nation (cost=0.00..1.31 rows=1 width=4) Filter: (n_name = 'FRANCE'::bpchar) -> Bitmap Heap Scan on supplier (cost=79.29..2351.30 rows=4000 width=34) Recheck Cond: (s_nationkey = nation.n_nationkey) -> Bitmap Index Scan on i_s_nationkey (cost=0.00..78.29 rows=4000 width=0) Index Cond: (s_nationkey = nation.n_nationkey) -> Hash (cost=1665268.56..1665268.56 rows=59985856 width=8) -> Seq Scan on lineitem l2 (cost=0.00..1665268.56 rows=59985856 width=8) -> Index Scan using orders_pkey on orders (cost=0.43..7.26 rows=1 width=4) Index Cond: (o_orderkey = l1.l_orderkey) Filter: (o_orderstatus = 'F'::bpchar) -> Index Scan using i_l_orderkey on lineitem l3 (cost=0.56..17.26 rows=45 width=8) Index Cond: (l_orderkey = l1.l_orderkey) Filter: ((l_receiptdate > l_commitdate) AND (l_suppkey <> l1.l_suppkey)) (30 rows)