SQL Server Query: Fast with Literal but Slow with Variable

前端 未结 8 1824
清歌不尽
清歌不尽 2020-11-28 07:18

I have a view that returns 2 ints from a table using a CTE. If I query the view like this it runs in less than a second

SELECT * FROM view1 WHERE ID = 1
         


        
8条回答
  •  眼角桃花
    2020-11-28 07:48

    Came across this same issue myself and it turned out to be a missing index involving a (left) join on the result of a subquery.

    select *
    from foo A
    left outer join (
      select x, count(*)
      from bar
      group by x
    ) B on A.x = B.x
    

    Added an index named bar_x for bar.x

提交回复
热议问题