In what order does execution on WHERE and ON clauses work?

后端 未结 4 733
隐瞒了意图╮
隐瞒了意图╮ 2021-01-23 16:41

I was reading this page about APPLY:

http://sqlblog.com/blogs/alexander_kuznetsov/archive/2009/07/07/using-cross-apply-to-optimize-joins-on-between-conditions.aspx

4条回答
  •  生来不讨喜
    2021-01-23 17:18

    They are not the same queries so why would you expect the same response times

    If the two queries are returning a different number of rows then use a top X for a more fair comparison

    Query optimizer can get very smart (and it can get stupid)
    View the query plan to see what is going on

    My experience is the query optimize has a better chance of getting smart if you pull the conditions into the join

    SELECT s.StartedAt, s.EndedAt, c.AirTime 
     FROM dbo.Commercials s 
     JOIN dbo.Calls c  
       ON c.AirTime >= s.StartedAt 
      AND c.AirTime < s.EndedAt 
      AND c.AirTime BETWEEN '20080701' AND '20080701 03:00' 
      AND s.StartedAt BETWEEN '20080630 23:45' AND '20080701 03:00'
    

    If you just have a single join then the query optimizer may move a where early
    But if you have multiple joins I have never seen the query optimizer move a where early

提交回复
热议问题