SQL Server 会自动丢弃不必要的连接吗?
如果我向 SQL Server 2005 提交一个查询,其中包含许多 LEFT JOIN 子句,而其中所连接的表从未被引用,那么连接是否仍然会发生,或者 SQL Server 是否足够智能以丢弃它们?
显然它不能丢弃 INNER JOIN [错误的假设!请参阅答案],因为这可能会改变结果,但是它可以用于 LEFT JOIN 吗?
If I submit a query to SQL Server 2005 which contains a number of LEFT JOIN clauses where the table joined to is then never referenced, will the joins still happen or is SQL Server intelligent enough to discard them?
Obviously it wouldn't be able to discard INNER JOINs [false assumption! see answers] as that would potentially change the result, but can it do it for LEFT JOINs?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它可以通过检查约束来丢弃 INNER JOIN 和 LEFT JOIN。如果您不使用表中的列,并且通过 (FK) 约束保证单行存在,则不需要使用该表。显然,这取决于查询,该表可能仍然被使用,因为它是最佳计划。
这是约束非常有用并且应该的另一个原因比设计中经常考虑的频率更高。
It can discard both INNER and LEFT JOINs by inspecting the constraints. If you don't use a column in the table and there is a guaranteed single row existence by (FK) constraint, then the table does not need to be used. Obviously it depends on the query, it's possible that the table still gets used because it's the best plan.
This is yet another reason that constraints are tremendously useful and should be considered more frequently than they often are in designs.
如果有很多匹配项,左连接可能会增加结果集。
因此仍然会对其进行评估。
A left join could potentially multiply your result set if there are many matches.
So therefore it would still be evaluated.