SQL poor stored procedure execution plan performance - parameter sniffing

后端 未结 3 992
花落未央
花落未央 2020-11-29 07:17

I have a stored procedure that accepts a date input that is later set to the current date if no value is passed in:

CREATE PROCEDURE MyProc
    @MyDate DATET         


        
相关标签:
3条回答
  • 2020-11-29 07:36

    One way I was able to get around this problem in (SQL Server 2005) instead of just masking the parameters by redeclaring local parameters was to add query optimizer hints.

    Here is a good blog post that talks more about it: Parameter Sniffing in SqlServer 2005

    I used: OPTION (optimize for (@p = '-1'))

    0 讨论(0)
  • 2020-11-29 07:41

    Basically yes - parameter sniffing (in some patch levels of) SQL Server 2005 is badly broken. I have seen plans that effectively never complete (within hours on a small data set) even for small (few thousand rows) sets of data which complete in seconds once the parameters are masked. And this is in cases where the parameter has always been the same number. I would add that at the same time I was dealing with this, I found a lot of problems with LEFT JOIN/NULLs not completing and I replaced them with NOT IN or NOT EXISTS and this resolved the plan to something which would complete. Again, a (very poor) execution plan issue. At the time I was dealing with this, the DBAs would not give me SHOWPLAN access, and since I started masking every SP parameter, I've not had any further execution plan issues where I would have to dig in to this for non-completion.

    In SQL Server 2008 you can use OPTIMIZE FOR UNKNOWN.

    0 讨论(0)
  • 2020-11-29 07:48

    Declare the procedure parameter inside the procedure and pass the external parameter to the internal .. compile ..

    0 讨论(0)
提交回复
热议问题