如果来自 table 的列没有被 SELECT 编辑,JOIN 会发生什么情况?

What happens to JOIN if no colums from said table are SELECT-ed?

我想知道 Oracle 或数据库管理器如何处理更长的脚本和更大的结果。我将在下面给出一个例子和一些问题:

SELECT order.nr, order.date, order.datesent, items.id, items.name
FROM orders
  LEFT JOIN items ON orders.itemid=items.id
  LEFT JOIN customer ON orders.custid=customer.id
  LEFT JOIN shipper ON order.shipperid=shipper.id
  LEFT JOIN production ON production.itemid=items.id
  LEFT JOIN sales ON sales.orderid=orders.id
  LEFT JOIN transport ON orders.transid=transport.id

Does the script run through all tables even though I don't use them in the SELECT?

SQL 引擎将处理所有的列值,因为它需要知道在任何 table 中是否有重复的 id 值,然后,如果有,是输出中的重复行。但是,它不一定执行完整的 table 扫描,因为它始终可以使用索引(假设您连接到索引列)。

如果联接的 table 的 id 列上有一个 UNIQUE 索引,那么从理论上讲,优化器可能会决定不理会不必要的联接绝不重复,数据不返回;但同样可能未使用该优化或保证该条件的索引不存在。

最好的解决方案是查看查询的 EXPLAIN PLAN,然后您将看到实际执行了哪些连接以及是否使用了 table 或索引扫描。