简体   繁体   English

使用INNER JOIN作为过滤器的性能和优点/缺点

[英]Performance and Pros/Cons of using INNER JOIN as filter

I have been working on MS SQL Server and has been following standard SQL Query to do INNER JOIN and filter the resultset based on the criteria. 我一直在研究MS SQL Server,并且一直在遵循标准的SQL查询来进行INNER JOIN并根据条件过滤结果集。

Recently, I was working with a large table where it has somehow cause the whole query to take ridiculous long time to do a query. 最近,我正在处理一个大表,该表以某种方式导致整个查询花费大量时间来进行查询。

Below is just a mock query of the situation. 下面只是对情况的模拟查询。

Select * FROM Customer a
INNER JOIN ProcessTable PT WITH(NOLOCK) on PT.custID = a.ID
INNER JOIN AuditTrailTable AT WITH(NOLOCK) on AT.custID = a.ID
WHERE AT.DATESTAMP > 'Some date time'

My colleague has come up with a new query as below 我的同事提出了一个新的查询,如下所示

Select * FROM Customer a
INNER JOIN ProcessTable PT WITH(NOLOCK) on PT.custID = a.ID
INNER JOIN AuditTrailTable AT WITH(NOLOCK) on AT.DATESTAMP > 'Some date time' AND AT.custID = a.ID

in which I observed he has put the filter clause as part of the INNER JOIN. 在其中,我观察到他已将filter子句作为INNER JOIN的一部分。

Despite it returns the same result, I would like to know the performance gain of this approach and pros and cons that I have not aware of please. 尽管它返回了相同的结果,但我想知道这种方法的性能提升以及我尚未意识到的优缺点。 Will it be a good approach of using this? 使用它会是一个好方法吗?

The two should be exactly the same. 两者应该完全相同 SQL Server's compiler will look at the queries and realize they are describing the same result set. SQL Server的编译器将查看查询,并意识到它们正在描述相同的结果集。 It will then optimize the query plan based on the data. 然后,它将根据数据优化查询计划。

Whether the condition goes in the on clause or the where clause does not matter for an inner join . 条件是在on子句中还是在where子句中对inner join联接都无关紧要。 (It does matter for an outer join .) (这对于outer join确实很重要。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM