stored procedure running slower than sql query

  • Hi

    I have one stored procedure and its taking 10 mins to execute. My stored procedure has 7 input parameters and one temp table( I am getting the data into temp table by using the input parameters) and also I used SET NOCOUNT ON. But if  copy the whole code of the SP and execute that as regular sql statement in my query analyzer I am getting the result in 4 seconds. I am really puzzled with this.

    What could be the reason why the SP is taking more than query,Unfortunately I can't post the code here.

    Thanks.

  • Could you post the text of the proc ?



    A.J.
    DBA with an attitude

  • Is it possible the queries table(s) have a gazillion rows now, but had zero or few rows when you compiled the procedure?  I'm wondering if the execution plan is stuck on stupid, thinking it doesn't need to use an index.

  • Do a search for parameter sniffing, you'll see what to do to fix this problem.

  • Even I tried by executing the SP 'with recompile' Option. Still its taking the same amount of time.

    Thanks.

  • Can you post the execution plans?

  • how can copy, paster the execution plan?

  • SET SHOWPLAN_TEXT ON

    GO

    Select 'query here'

    GO

    SET SHOWPLAN_TEXT OFF

  • Even with recompile you can suffer the same result from parameter sniffing.

    in your procedure reassign in parameters to a new parameter, and use the new parameter in the query.

    also dbcc dropcleanbuffers

    dbcc freeproccache

    Snippit from this link

    http://www.microsoft.com/technet/prodtechnol/sql/2005/recomp.mspx#EGAA

    Compilations, Recompilations, and Parameter Sniffing

    "Parameter sniffing" refers to a process whereby SQL Server's execution environment "sniffs" the current parameter values during compilation or recompilation, and passes it along to the query optimizer so that they can be used to generate potentially faster query execution plans. The word "current" refers to the parameter values present in the statement call that caused a compilation or a recompilation. Both in SQL Server 2000 and SQL Server 2005, parameter values are sniffed during compilation or recompilation for the following types of batches:

    Stored procedures

    Queries submitted via sp_executesql

    Prepared queries

    In SQL Server 2005, the behavior is extended for queries submitted using the OPTION(RECOMPILE) query hint. For such a query (could be SELECT, INSERT, UPDATE, or DELETE), both the parameter values and the current values of local variables are sniffed. The values sniffed (of parameters and local variables) are those that exist at the place in the batch just before the statement with the OPTION(RECOMPILE) hint. In particular, for parameters, the values that came along with the batch invocation call are not sniffed.

  • I had a SP that was taking longer and I found out the optional parameter was doing it.  If I supplied the value it went in 30 seconds.  If I used a default it took several minutes.

  • I had a select with a lot of joins in my sp.

    usaly this step took 2 seconds for 200000 records. I wrote the time to a debug table.

    With little ram available it could take an hour.

    with option recompile at the and of the query in the stored procedure i don't have the problem any longer.

    It just goes as fast a in the query analyzer.

    select *

    from table

    option recompile

    The rest of my stored procedure is still recompiled.

  • Ditto here. I supplied a value to my optional parm and Wammo! Super fast. Why do optional parameters cause this slow down?

  • Could be that when the optional parameter is missing, the query in the stored procedure is using an inefficient query plan.

  • A little addition here. I was, obviously, having this problem as well. Here is an example of what my proc looked like:

    CREATE PROC Reports.MyExampleProc

    @StartDate DateTime,

    @EndDate DateTime

    AS

    IF(@EndDate is null) SET @EndDate = getDate()

    [DO query here that uses a between statement]

    I rewrote this to the following:

    CREATE PROC Reports.MyExampleProc

    @StartDate DateTime,

    @EndDate DateTime

    AS

    DECLARE @NewEndDate

    IF(@EndDate is null) SET @NewEndDate = getDate()

    [DO query here that uses a between statement using the @NewEndDate variable instead of @EndDate]

    And performance matched what I expected

  • Chris Rogers (11/24/2008)


    A little addition here. I was, obviously, having this problem as well. Here is an example of what my proc looked like:

    CREATE PROC Reports.MyExampleProc

    @StartDate DateTime,

    @EndDate DateTime

    AS

    IF(@EndDate is null) SET @EndDate = getDate()

    [DO query here that uses a between statement]

    I rewrote this to the following:

    CREATE PROC Reports.MyExampleProc

    @StartDate DateTime,

    @EndDate DateTime

    AS

    DECLARE @NewEndDate

    IF(@EndDate is null) SET @NewEndDate = getDate()

    [DO query here that uses a between statement using the @NewEndDate variable instead of @EndDate]

    And performance matched what I expected

    I see the potential for a slight change:

    CREATE PROC Reports.MyExampleProc

    @StartDate DateTime,

    @EndDate DateTime

    AS

    DECLARE @NewEndDate

    --IF(@EndDate is null) SET @NewEndDate = getDate()

    SET @NewEndDate = coalesce(@EndDate, getdate()) -- or isnull(@EndDate,getdate())

    -- use @NewEndDate in your query instead of EndDate

    --[DO query here that uses a between statement using the @NewEndDate variable instead of @EndDate]

Viewing 15 posts - 1 through 15 (of 17 total)

You must be logged in to reply to this topic. Login to reply