简体   繁体   English

操作顺序SQL Server中的内部联接和Where子句性能?

[英]Order of operation Inner Join and Where clause performance in SQL Server?

Is there any Performance problem to use operation in miss order? 在按错过顺序使用操作时是否存在任何性能问题?

Like 1. All Inner join first then all where condition later. 如图1。所有内部成员先加入,然后所有条件加入。

    select * from
    t1
    inner join t2 on t1.t2Id = t2.Id
    inner join t3 on t1.t3Id = t3.Id
    inner join t4 on t2.t4Id = t4.Id
    where 
    t1.Id in (1,2,3,4,5)
    and t2.Id in (1,2,3,4,5,6,7)
    and t3.Name like '%a' 

2. All table with Respective Where and then Inner join 2.所有表分别带有“何处”和“内部”联接

 select * from
    (select * from t1 where t1.Id in (1,2,3,4,5)) a
    inner join (select * from t2 where t2.Id in (1,2,3,4,5,6,7)) a1 on a.t2Id = 
    a.Id
    inner join (select * from t3 where t3.Name like '%a') a2 on a.t3Id = a2.Id
    inner join t4 on a1.t4Id = t4.Id

It may effect on query Performance? 它可能会影响查询性能吗?

  1. Also Order of Where Condition? 还订购何处条件?

    Like 喜欢

     select * from t1 inner join t2 on t1.t2Id = t2.Id where t1.t2Id in (1,2,3,4,5,6) and t2.t3Id in (1,2,3,4,5) 

A SQL query goes through three phases when it is run: SQL查询在运行时会经历三个阶段:

  1. The query is parsed (and the various references are looked up). 解析查询(并查找各种引用)。
  2. The execution plan is created, with an optimization phase based on what the query needs to accomplish. 创建执行计划,并根据查询需要完成的内容进行优化。
  3. The query plan is execution. 查询计划正在执行。

As a result of the optimization, the way you write the query often has less effect on the performance than you might think. 作为优化的结果,编写查询的方式通常对性能的影响比您想象的要小。 Lots of people have worked very hard on figuring out the best way to optimize queries -- and there are probably lots of things that you are not even aware of (such as different join algorithms, join ordering, pushing down expression evaluations, and so on). 很多人都在努力寻找优化查询的最佳方法,这很辛苦-可能还有很多您甚至不知道的事情(例如不同的联接算法,联接顺序,下推表达式求值等等) )。

For your examples, the SQL Server optimizer should produce the same execution plans. 对于您的示例,SQL Server优化器应产生相同的执行计划。 The engine is smart enough to realize that these are really doing the same thing. 引擎足够聪明,可以意识到它们确实在做同样的事情。

Note: This is not true of all query engines. 注意:并非所有查询引擎都适用。 Some have pretty poor optimizers, and there would be differences in performance. 有些优化器的性能很差,性能会有差异。

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

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