Tuesday, February 8, 2011

SQL Server 9.0 - Stored Procedures performance degrades over time

There's an odd problem with some stored procedures we have in SQL Server 9.  When the query is first published to the server the performance runs in 2-3 seconds.  Over time the queries continue to become slower and slower until they are pretty much unusable.  It stays unusable until you drop and recreate the stored procedure.

We have found that if you have input parameters and those parameters are used in the query that this is at least one cause of the problem.  Not sure why this is but we have found a work around.

If you create local variables and then assign those local variables the value of the parameter that you passed in this problem goes away for the affected stored procedures.

Instead of:

CREATE procedure test_query
(
    @input_param as varchar(10)
)

AS
BEGIN

   SELECT * from test_table where test_field = @input_param

END

You can code:



CREATE procedure test_query
(
    @input_param as varchar(10)
)

AS

BEGIN

   DECLARE @local_input_param as varchar(10)

   SET @local_input_param = @input_param

   SELECT * from test_table where test_field = @local_input_param

END

No comments:

Post a Comment