T-SQL Where Clause Case Statement Optimization (optional parameters to StoredProc)

后端 未结 5 1369
灰色年华
灰色年华 2021-02-05 10:54

I\'ve been battling this one for a while now. I have a stored proc that takes in 3 parameters that are used to filter. If a specific value is passed in, I want to filter on that

5条回答
  •  执念已碎
    2021-02-05 11:29

    SQL 2008 and later make some improvements to optimization for things like (MyCase IS NULL OR MyCase = @MyCaseParameter) AND ....

    If you can upgrade, and if you add an OPTION (RECOMPILE) to get decent perf for all possible param combinations (this is a situation where there is no single plan that is good for all possible param combinations), you may find that this performs well.

    http://blogs.msdn.com/b/bartd/archive/2009/05/03/sometimes-the-simplest-solution-isn-t-the-best-solution-the-all-in-one-search-query.aspx

提交回复
热议问题