sp VS adhoc query peformance?

  • I have a issue i am struggling with. I have a sp that returns 1700 records in about 1 minute but when i run the same exact sql in an adhoc query it takes on 5 seconds?

    I declare a table variable for a summary table, load the table then join the results with 4 other tables. It doesn't seem to be anything out the ordinary but the sp will timeout when running via the applcation.

    I seem to be just banging my head against the wall at this point! Any assistance is appreciated.

  • Does your table variable have a primary key and is that used to join to the other tables? If not, that's going to be a performance problem right there since table variables don't maintain statistics. You may want to switch to a temporary table instead.

    If it's an ad hoc query you may be seeing recompiles. Have you watched the execution through profiler?

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • It seems rather extreme - but could it be parameter sniffing?

    Ken Henderson (you will be missed) described it rather well here:

    http://blogs.msdn.com/khen1234/archive/2005/06/02/424228.aspx

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • I wonder if the procedure hasn't recompiled since your last change and is using an old execution plan? Might want to try doing a freeproccache for s&g's ...

    Regardless, have you looked at the execution plan with the procedure and compared it to the execution plan when running it via TSQL?

  • And, check your connection settings. See if the TSQL connection & the ADO connection are the same.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

  • Thanks for all your responses.

    This issue appears to be wide spread and happening on several stored procedures. Now even my adhoc queries are taking extremely long time to return results.

    Just a little background, we just moved several dB's to production from our test. Both environments are sql server 2005. We did not have any issues in the test environment.

    I am far from a DBA, but I have never encountered these performance issues from copying stored procedures from one server to anohter.

    Has anyone heard of any issues similar to this?

  • First and foremost, you need to look at your execution plans ... your problem could be one of MANY things at this point.

    Run your query in query analyzer/management studio, with execution plan turned on. You'll get a new output tab showing the execution plan. Look for the highest percentages used from step to step to start determining where the most resources are used.

  • Check the sys.dm_exec_requests DMV. What are the long running queries waiting on?

    Have you rebuilt indexes recently?

    Does recompiling the procedure make any difference? (EXEC sp_recompile 'StoredProcName')

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • Silly question, but how much more data do you have in production compared to your test environment? Is your test data obviously fake test data such as lots of similar names, etc?

  • >>Has anyone heard of any issues similar to this?

    Yeah, we all have! :-)) The problem is that the potential causes are quite numerous. Even without knowing your code or data structures or data distributions I could probably rattle off 15 or 20 different possibles. In situations like this there is no substitute for experience - it's gonna be a painful process going back and forth via forum posts. Don't suppose you have a DBA at work you can hit up? 10-20 minutes of their time (if that) online with you data should be all it takes to isolate the cause.

    Best,
    Kevin G. Boles
    SQL Server Consultant
    SQL MVP 2007-2012
    TheSQLGuru on googles mail service

  • Hi,

    I had the same problem with some SPs. The problem was, that I used a variable (it was a parameter of the SP) directly in the select statement in a string operation... :ermm:

    For example:

    Create Proc dbo.Test (@Parameter1 AS NVARCHAR(100))

    AS

    BEGIN

    ...

    SELECT ID, LastName + ' ' + @Parameter1

    FROM Adresses

    ...

    END

    This caused cach missend and a recompile of the complete SP...

    So I declared a local variable and put the Parameter in that variable and it worked... 🙂

    For example:

    Create Proc dbo.Test (@Parameter1 AS NVARCHAR(100))

    AS

    BEGIN

    DECLARE @Local NVARCHAR(100)

    SET @Local = @Parameter1

    ...

    SELECT ID, LastName + ' ' + @Local FROM Adresses

    ...

    END

  • All,

    Thanks for your assistance. I isolated the issue to one table. It contains over 1 million records and someone had not added the keys & indexes. Since there was no key on the table dups were created.

    I had re-written several of the stored procedures that ran against this table and did get the performance i had originally seen. It was by isolating the result set into a temp table then joining all of the other lookup tables. I also used local variables instead of the parms as someone suggested.

    I de-dup'd the table with one of the scripts I found here at SQL Server Central. A big thanks to the folks that share their code with us!!! After adding the table's constraints life is good again. We are in the process of re-checking all of the database and performance tuning unitl we can get a dba.

    Thanks again for all your responses and help 🙂

  • Sounds like you just made yourself one great argument for a tool that will compare the schemas of two databases, show you the differences and then script the necessary changes.

    There are a bunch of them on the market but it just so happens that redgate (see banner at top of page) makes a great one called SQL Compare - been using it for years and loving it.

    Just for appearances (not too mercenary, me) there are some other good ones from the folks at ApexSQL, Idera (Change Manager is worth a look as well) and even MS with the DBA version of Team Studio.

    If you spent more than 15 minutes fixing this problem (sounds like it was hours & hours) a tool would have paid for itself for sure.

    Joe

  • After a restore and after having created all the necessary indexes, try using the following code on your database:

    exec sp_MSforeachtable 'update statistics ? with FullScan, Norecompute'

    Best Regards,

    Suresh.


    Kindest Regards,

    M Suresh Kumar

  • Please note: 3 year old thread.

    Why would you recommend setting every single statistic in the database to not get automatically updated again? Very dangerous setting without some manual stats updates also recommended.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass

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

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