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

前端 未结 11 1123
失恋的感觉
失恋的感觉 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 04:03

    its due to parameter sniffing. first of all declare temporary variable and set the incoming variable value to temp variable and use temp variable in whole application here is an example below.

    ALTER PROCEDURE [dbo].[Sp_GetAllCustomerRecords]
    @customerId INT 
    AS
    declare @customerIdTemp INT
    set @customerIdTemp = @customerId
    BEGIN
    SELECT * 
    FROM   Customers e Where
    CustomerId = @customerIdTemp 
    End
    

    try this approach

提交回复
热议问题