Why does putting a WHERE clause outside view have terrible performance

前端 未结 5 1147
萌比男神i
萌比男神i 2021-02-12 21:00

Let\'s say you have a view:

CREATE VIEW dbo.v_SomeJoinedTables AS
SELECT
    a.date,
    a.Col1,
    b.Col2,
    DENSE_RANK() 
      OVER(PARTITION BY a.date, a.         


        
5条回答
  •  有刺的猬
    2021-02-12 21:17

    I'm not a SQL expert, so I may be voted down for my foolishness, but my guess is that in the first case SQL is fetching the results of the entire view before applying the predicate in the WHERE clause. So when you query the view, it selects all of the records, puts them in memory, and then applies the Date filter after it is done.

    This seems similar to the way the entire data set specified in your joins is fetched prior to applying the filter in the WHERE (lesson here is that you should apply predicates in your ON clause when possible).

    Unless views are treated differently somehow.

提交回复
热议问题