SP taking 15 minutes, but the same query when executed returns results in 1-2 minutes

前端 未结 11 1138
失恋的感觉
失恋的感觉 2021-02-01 03:22

So basically I have this relatively long stored procedure. The basic execution flow is that it SELECTS INTO some data into temp tables declared with he #

11条回答
  •  花落未央
    2021-02-01 03:52

    This is the footprint of parameter-sniffing. See here for another discussion about it; SQL poor stored procedure execution plan performance - parameter sniffing

    There are several possible fixes, including adding WITH RECOMPILE to your stored procedure which works about half the time.

    The recommended fix for most situations (though it depends on the structure of your query and sproc) is to NOT use your parameters directly in your queries, but rather store them into local variables and then use those variables in your queries.

提交回复
热议问题