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

前端 未结 11 1117
失恋的感觉
失恋的感觉 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:44

    I too faced a problem where we had to create some temp tables and then manipulating them had to calculate some values based on rules and finally insert the calculated values in a third table. This all if put in single SP was taking around 20-25 min. So to optimize it further we broke the sp into 3 different sp's and the total time now taken was around 6-8 mins. Just identify the steps that are involved in the whole process and how to break them up in different sp's. Surely by using this approach the overall time taken by the entire process will reduce.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-01 03:53

    For a start it doesn't sound like the SQL is going to perform too well anyway based on the use of a number of temp tables (could be held in memory, or persisted to tempdb - whatever SQL Server decides is best), and the use of cursors.

    My suggestion would be to see if you can rewrite the sproc as a set-based query instead of a cursor-approach which will give better performance and be a lot easier to tune and optimise. Obviously I don't know exactly what your sproc does, to give an indication as to how easy/viable this is for you.

    As to why the SP is taking longer than the query - difficult to say. Is there the same load on the system when you try each approach? If you run the query itself when there's a light load, it will be better than when you run the SP during a heavy load.

    Also, to ensure the query truly is quicker than the SP, you need to rule out data/execution plan caching which makes a query faster for subsequent runs. You can clear the cache out using:

    DBCC FREEPROCCACHE
    DBCC DROPCLEANBUFFERS
    

    But only do this on a dev/test db server, not on production. Then run the query, record the stats (e.g. from profiler). Clear the cache again. Run the SP and compare stats.

    0 讨论(0)
  • 2021-02-01 03:54

    I'd also look into parameter sniffing. Could be the proc needs to handle the parameters slighlty differently.

    0 讨论(0)
  • 2021-02-01 03:55

    I usually start troubleshooting issues like that by using "print getdate() + ' - step '". This helps me narrow down what's taking the most time. You can compare from where you run it from query analyzer and narrow down where the problem is at.

    0 讨论(0)
  • 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

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