SQL Server where 语句中带条件的交叉联接和 join's on 语句中带条件的内联接的性能比较

发布于 2024-08-10 09:14:10 字数 439 浏览 4 评论 0原文

我试图确定在 SQL Server 2005 中运行的一段生成的 SQL 的性能。

它使用交叉联接,但绑定交叉联接表的条件位于 where 语句中。

我之前认为所有具有 where 语句的交叉联接都会首先提取完整的笛卡尔积,然后应用过滤器。

然而,MSDN 上的以下链接给出了不同的建议。

https://msdn.microsoft.com/en-us/library/ms190690。 aspx

它特别指出,如果交叉连接表上有条件,它将“表现”像内部连接。接下来显示了内部联接和带有条件的交叉联接的类似结果的示例。

但它没有说明性能差异是什么,只是说它们的行为方式相似。

I am trying to determine the performance of a piece of generated SQL that runs in SQL Server 2005.

It uses CROSS JOINS, but the conditionals tying the cross joined tables are in the where statement.

I previously thought that all cross joins that have where statements would first pull the full cartesian product and then apply the filter.

However, this following link on MSDN suggests differently.

https://msdn.microsoft.com/en-us/library/ms190690.aspx

It specifically states that if there is a conditional on a cross joined table, it will "behave" like an inner join. It goes on to show an example of the similar results of an inner join and a cross join w/ where conditional.

It does not state what the performance difference is though, only that they behave in a similar fashion.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮茶煮酒煮时光 2024-08-17 09:14:10
The example given is correct, atleast in a basic query. 

下面的查询将为两个查询生成相同的执行计划和执行时间。 Sql 优化器通常可以在连接操作期间自由地在 where 子句和“on”子句之间移动限制,我想交叉连接也是如此。

    select companies.id, contacts.id
from 
companies
cross join contacts
where
contacts.companyid = companies.id


select companies.id, contacts.id
from 
companies
inner join contacts on contacts.companyid = companies.id
The example given is correct, atleast in a basic query. 

The query below will yield the same execution plan and execution times for both queries. The Sql Optimiser is typically free to move restrictions between where clauses and 'on' clauses during join operations, and i imagine the same is for cross joins.

    select companies.id, contacts.id
from 
companies
cross join contacts
where
contacts.companyid = companies.id


select companies.id, contacts.id
from 
companies
inner join contacts on contacts.companyid = companies.id
谈场末日恋爱 2024-08-17 09:14:10

也许最简单的事情就是显示语句的执行计划并查看它正在做什么。您可以从 Management Studio 执行此操作 - 这里有更多信息:

http:// www.sql-server-performance.com/tips/query_execution_plan_analysis_p1.aspx

Probably the easiest thing to do is to display the execution plan for your statement and take a look at what it's doing. You can do it from management studio - there's some more info here:

http://www.sql-server-performance.com/tips/query_execution_plan_analysis_p1.aspx

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文