Query tuning performance

  • how can we get execution plan of a query executed on two different days so that i come to know that what is cause of slowing down the query today? i know that query was running fine one week back.

    what are the steps to find the cause of slowing down the query today which was running very well in past?

    what could be the possible reason of slowing down the query today which was running very well in past?

    I have faced this question in interview lot of time could you please help me?

  • Query slowness is due to serveral reasons

    poor Statistics or index or disk or network need to have run trace and identify .

    for interview questions you have get through experience not an answer from experts.

    Regards
    Durai Nagarajan

  • There is no way to go back in time to retrieve an execution plan. So, if you have a need to monitor particular plans, you need to set up, ahead of time, a mechanism for capturing those plans. When I have to do this, I generally use extended events. It is possible to use a server-side trace, but they put a lot of load on the server. You can also query directly into the cache using the Dynamic Management Objects. But you'll have to set all this up.

    Conversely, if you know the query has a problem now, capture the plan (see above) and save it. You can save plans as .sqlplan files. Then, either wait for performance to go back to normal or force a recompile on the query in order to see if changing circumstances change the plan. Compare what's there to what you captured.

    But, performance may not be about plans changing. It could be about differences in statistics, the amount of data, blocking, etc. So all this comes back to having monitoring in place to be able to track system performance and behavior.

    "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

  • There are a multitude of possible reasons why the query started to run slower.

    But a very common scenario is that you have a query which involves something like "today's orders". When statistics have recently been updated, the optimizer can deal that there are a fair share of orders today and will arrive to a plan accordingly. But as the week moves on, statistics are not updated, neither manually, nor automatically beause the table is too big to trigger autostats very often. Now when you ask for today's orders, the optimizer looks at the statistics and finds nothing and estimate 1 order. And then it goes downhill from there.

    Often yoy may be lucky; if the query is inside a stored procedure stays in cache for a long time, why the bad statistics does not matter. But one day, the plan goes out of cache for whatever reason, for instance a server restart.

    [font="Times New Roman"]Erland Sommarskog, SQL Server MVP, www.sommarskog.se[/font]

  • Check out : http://ola.hallengren.com/

    Learn the and understand the code.

    When you do, you'll have no worries with these questions again.



    For better, quicker answers on T-SQL questions, read Jeff Moden's suggestions.[/url]

    "Million-to-one chances crop up nine times out of ten." ― Terry Pratchett, Mort

  • Dennis Post (9/10/2013)


    Check out : http://ola.hallengren.com/

    Learn the and understand the code.

    When you do, you'll have no worries with these questions again.

    Where in the linked site can I find out how to interpret and compare execution plans, Dennis?

    “Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

    For fast, accurate and documented assistance in answering your questions, please read this article.
    Understanding and using APPLY, (I) and (II) Paul White
    Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden

  • :unsure:

    Hmm good point Chris.

    Not sure why I posted that.

    Good to keep indexes defragmented and statistics up to date though....



    For better, quicker answers on T-SQL questions, read Jeff Moden's suggestions.[/url]

    "Million-to-one chances crop up nine times out of ten." ― Terry Pratchett, Mort

Viewing 7 posts - 1 through 6 (of 6 total)

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