Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 

Gold in the Execution Plan hills

By Tony Davis,

Many people, when examining the execution plan for a malfunctioning query, tend to focus largely on the usual set of "suspect" operators, such as Lookups, Spools, Sorts, and Index Scans (rather than Seeks), and then use a mixture of indexing and query rewriting to try to make the problem go away. Some success can be had with this approach, but it tends to fare less well when one is confronted with really knotty problems, such as the erratically performing query; the one that performs admirably within SSMS but terribly when executed from the application, or the one that having caused no previous trouble suddenly starts to misbehave.

What I came away with, from attending several execution plan-related sessions at the recent PASS summit, from the likes of Grant Fritchey, Gail Shaw, Adam Machanic and Klaus Aschenbrenner was a renewed appreciation of, firstly, how often erratic or unpredictable query performance is related to issues surrounding data skew, statistics and parameter sniffing, and secondly, how much information regarding these issues is available directly from the execution plan, but tucked away in the Property sheets, and behind seemingly "trivial" operators, such as SELECT, to which I'd previously paid little attention.

Did you know, for example, that the properties for the SELECT operator of an exertion plan reports a "Reason for early termination of statement optimization"? If you see the value "Time out" here, it's a cause for concern, as it means the optimizer failed to find a "good enough" plan in the time allotted.

Also hiding in the properties of the SELECT operator, are the exact values of the SET options used for the query execution. Differing values for the various SET options, between execution environments is a classic cause of procedure that has a good plan in SSMS, but a bad plan when executed from a client app. If the same procedure is executed with different SET options then any existing plan for that procedure will not be reused. Instead a new plan will be generated, and if the parameter values sniffed this time happen to be highly non-representative of "normal" execution, then you'll get a bad plan and poor performance. Just such an issue is reported in this issue of Database Weekly. In this case, the author tracked the problem down using DMVs and plan handles, but in fact, you can compare the SET options directly, by generating the actual execution plan in each case, and reading the values from the SELECT operator.

Here's another little piece of information behind the SELECT operator of which I was previously unaware: Memory grant. Let's say we have a hash or sort operation that the optimizer predicts will work on 5 rows, based on available statistics. A correspondingly-small amount of memory will be granted to perform that operation, and you'll see this memory grant in the SELECT operator of the execution plan. If it turns out that the stats are inaccurate, and the operation actually returns 500 rows, it can't suddenly request more memory, and so the operation will simply spill to disk, in tempdb. It's worth capturing actual execution plans (as opposed to estimated, which is what's stored in the plan cache), and keeping an eye on wild disparities between the estimated and actual number of rows returned by a given operation. It's likely that outdated stats are the cause; they are only auto-updated when" 20% + 500 rows" of the data in a table have changed. For tables with millions of rows, this means that data skew can change drastically before the stats update automatically.

It takes a while to master the art of reading and understanding execution plans, to know exactly where to look for the information, and to understand not only what "dodgy" operators to look out for, but also the 'hidden' properties of each operator that will help you understand the flow of data through the plan. If it's true that it takes 10,000 hours of training to master SQL Server, then you probably need to invest several tens of those hours mastering execution plans.

Cheers,
Tony.

Total article views: 201 | Views in the last 30 days: 2
 
Related Articles
FORUM

Query Execution Performance

Query Execution Performance

BLOG

Estimated rows, actual rows and execution count

It’s often said that a major discrepancy between estimated and actual row counts in a query’s execut...

FORUM

Syntax error on executing this Performance tuning query

Syntax error on executing this Performance tuning query

FORUM

Query Slow Performance

Query Slow Performance

FORUM

SQL Query Performance Help !

SQL Query Performance Help !

Tags
editorial    
execution plans    
 
Contribute

Join the most active online SQL Server Community

SQL knowledge, delivered daily, free:

Email address:  

You make SSC a better place

As a member of SQLServerCentral, you get free access to loads of fresh content: thousands of articles and SQL scripts, a library of free eBooks, a weekly database news roundup, a great Q & A platform… And it’s our huge, buzzing community of SQL Server Professionals that makes it such a success.

Join us!

Steve Jones
Editor, SQLServerCentral.com

Already a member? Jump in:

Email address:   Password:   Remember me: Forgotten your password?
Steve Jones