WHERE clause better execute before IN and JOIN or after

前端 未结 5 1504
感动是毒
感动是毒 2020-11-30 02:12

I read this article: Logical Processing Order of the SELECT statement

in end of article has been write ON and JOIN clause consider before WHERE.

Consider we

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-30 02:58

    Just re-reading Paul White's excellent series on the Query Optimiser and remembered this question.

    It is possible to use an undocumented command to disable specific transformation rules and get some insight into the transformations applied.

    For (hopefully!) obvious reasons only try this on a development instance and remember to re-enable them and remove any suboptimal plans from the cache.

    USE AdventureWorks2008;
    
    /*Disable the rules*/
    DBCC RULEOFF ('SELonJN');
    DBCC RULEOFF ('BuildSpool');
    
    
     SELECT  P.ProductNumber, 
             P.ProductID, 
            I.Quantity
     FROM    Production.Product P
     JOIN    Production.ProductInventory I
             ON  I.ProductID = P.ProductID
    WHERE I.ProductID < 3
    OPTION (RECOMPILE)
    

    You can see with those two rules disabled it does a cartesian join and filter after.

    Rules Off Plan

    /*Re-enable them*/   
    DBCC RULEON ('SELonJN');
    DBCC RULEON ('BuildSpool');
    
     SELECT  P.ProductNumber, 
             P.ProductID, 
            I.Quantity
     FROM    Production.Product P
     JOIN    Production.ProductInventory I
             ON  I.ProductID = P.ProductID
    WHERE I.ProductID < 3
    OPTION (RECOMPILE)
    

    With them enabled the predicate is pushed right down into the index seek and so reduces the number of rows processed by the join operation.

    Rules on Plan

提交回复
热议问题