May 1, 2015 10 Comments
Execution Plan Instability – What is the problem?
The Oracle Optimizer is a complex piece of software and with every release it becomes more complex.
In the beginning, the Optimizer was rule-based.
The Optimizer had a ranked list of heuristics used to optimize a query, picking the lowest ranked rule available.
This rule-based mode, whilst still in use with some internal Oracle dictionary queries, has been unsupported since version 10.1.
This means that no code changes have been officially made to the RBO and no bug fixes are provided. There are many features that the RBO is unaware of.
Applications should not still be using the rule-based optimizer.
The Cost-Based Optimizer is designed to evaluate a number of execution plans and pick the one estimated to be the fastest.
Many of the CBO features are designed to combat common optimization problems and many of these problems occur where development teams are not necessarily aware of the full implications of the features that they are using.
These built-in default behaviours roughly conform to the infamous 80%:20% rule in that most of the time they do a good job but they are not infallible.
Bind Variables, Literals and Histograms
Most of the features which are deliberately designed such that plan instability is difficult to avoid stem from the decision to use bind variables or literals (NOT that they should be mutually exclusive) and the interaction of the former with histograms.
In article 1 of his Philosophy series, Jonathan Lewis neatly sums this up:
Histograms and bind variables exist for diametrically opposed reasons – they won’t work well together without help
Fundamentally, bind variables exist to provide shareable plans.
Bind variables should be used where we are not interested in getting specific optimizations for differing parameters.
Literals should be used where we want the Optimizer to pay particular attention to data distribution and skew for the specific parameters supplied.
A SQL statement will/should often have some parameters which should be literals and some which should be binds.
From this point onwards, there has been a whole raft of features designed to treat the misuse of one or the other.
188.8.131.52, histograms were introduced.
Histograms exist to provide specific data distribution information, particularly relevant to specific parameters.
Also, in that version, we got CURSOR_SHARING – targeted at applications using literals instead of binds such that SQL which was identical part from the use of binds was rewritten to use sytem-generated bind variables.
Then in 9.2, we got bind variable peeking.
This feature was introduced so that the optimizer could peek at the values supplied at parse time and use data distribution information specific to these parse-time values to generate an execution plan which suited those values.
In addition and at the same time, through these various versions to present day, we have had the default behaviour of DBMS_STATS statistic gathering to let the database decide which columns it will create histograms on, based on the SQL which has been running.
This means that new histograms can suddenly spring up – or existing histograms unexpectedly disappear – on all sorts of columns. This can be problematic on columns with large numbers of distinct values AND particularly so on join cardinalities where there may be a mismatch of histograms on both sides of the join.
Ever since this point, we have had a conflict of interest in feature usage and an ever increasing number of additional functionality to battle against this conflict – adaptive cursor sharing, cardinality feedback, etc, etc
Finally, the education message got blurred or lost somewhere along the line to the extent that a lot of systems blindly overuse bind variables because of the perceived performance impact of using literals.
This situation is not helped by the fact PL/SQL is designed to encourage bind variables.
Using supplied parameters as literals means using some construct of dynamic SQL, not difficult but nevertheless an added complexity and also another feature which is often blindly discouraged.
SQL Execution Plan Instability – Is SPM a viable approach?
In Oracle 11g, Oracle introduced SQL Plan Baselines as a SQL Plan Management feature.
The core attraction of this functionality is that you can “lock in” a particular plan or plans for a SQL statement. This stores a set of outline hints and a specific plan hash value in relation to a specific SQL signature.
The Optimizer then uses that set of hints to try to reproduce the desired plan. If it can it will, if it can’t it will reject the hintset.
Additionally, the Optimizer completes its best-cost execution plan optimization anyway so that it can provide the ability to do a controlled evolution of baselined plans in the event that the lower-cost plan that it would have used is different and better performing than the baselined plan.
To use this the database parameters (session or system) just need to be configured to capture plans into a baseline and then use them.
There is flexibility to this capture/use. You can capture everything as it is generated; you could capture everything from memory now and/or at regular intervals and import into a baseline; you could capture everything from AWR into a SQL Tuning Set and then import into a baseline; or you could capture everything from another environment and export/import it into another.
And at any point, you can turn off capture and continue to use those which you currently have – this usage continues to capture any lower cost plans that the optimizer would have generated for any existing baselined plans
For a more detailed look at SPM and a comparison with SQL Profiles, see documentation.
Sounds good – why isn’t it the silver bullet for system-wide stability?
This approach might be good enough, but it is not a cast-iron guarantee of system-wide stability.
There are a number of reasons why not.
New & Changed SQL
Firstly, you need to have captured all your plans already. If you get brand new SQL, then there will be nothing in SPM.
Depending on your application, this may be a significant concern.
For example, consider an application making heavy usage of Hibernate which generates SQL statements.
A minor change in the domain model can mean a change of system-generated table alias in many statements.
As a result, you may suddenly get a lot of brand new SQL and significant numbers of baselined statements which you will now never see again.
What are the baselined plans based on? Are they the best plans? The only plans ever used?
If you suddenly baseline the plans for a large number of SQL statements, you are dictating which plan is to be used.
The plans will be based on the parsed plans in memory or in AWR at the time.
Are these the best plans?
Does/should this SQL statement always use this plan?
Are there normally multiple plans for different bindsets?
What if you normally get benefit from adapative cursor sharing?
ACS and baselines
What if your application benefits from adapative cursor sharing?
Sure, you can baseline multiple plans but these plans have no ACS information.
As soon as that ACS information is no longer in memory (as happens), there is no shortcut in a baseline to regain that, you still have to have the multiple executions required for the optimizer to recognize that which plans to use for which bindsets.
Widespread usage of baselines might, depending on your system performance profile, have a significant impact on the parsing resources.
This is because it always generates a best-cost plan anyway.
Then if that is not the baselined plan, it will use the baselined hintset to try to generate the specific plan hash.
In addition, it that is not possible, it will use just the optimizer_features_enable hint to try to generate the required plan.
So, you might in a heavily-baselined system to be doing 2x the parse work of a non-baselined system.
This might well be easily accommodated but there are systems where this would cause a problem.
Integration with development and testing processes
A SQL Plan Baseline is tied to a SQL statement based on the statement’s EXACT_MATCHING_SIGNATURE – a hash of the SQL statement which has been case and space normalized.
If a SQL statement materially changes, the baseline no longer applies.
How aware are developers of the presence of baselines?
And how to integrate with the development process?
How will our release process deal with baselines?
And if baselining large numbers of plans is being considered, then we have to think about where these will be generated.
The natural implication (of widespread baseline usage) is that new functionality being promoted to Prod would have a set of tested, baselined plans accompanying it and these would presumably have to be generated in a Prod-like environment which included Prod-like volumes.
SQL Execution Plan Instability – Decision Time
There is a decision to be made.
(And/or perhaps there is often a conceptual over-simplification by senior management to combat? Or at least a lack of deep understanding of the beast that we’re dealing with here?)
Do you want the Optimizer to try to get a better execution plan sometimes?
If the answer is yes, then you have to accept that it will get it wrong from time to time.
In particular, the various feedback and adaptive mechanisms are designed to recognize that they have got it wrong.
BUT they need that problematic execution in the the first place – sometimes more than one – to recognize that fact.
That one problematic execution could be your next Priority 1 incident.
In addition, the feedback mechanism is not perfect and it still can make subsequent executions worse in some circumstances.
SQL Execution Plan Instability – Turn it off?
IF your primary goal is plan stability – and I think many teams would claim this is this their goal but they do not embrace the implications of this – then perhaps a better decision is to turn off the various features which cause or combine to cause most of the problems of instability.
Appropriate usage of binds, appropriate usage of literals
Choose whether to use a bind variable or a literal as is appropriate for the value / column / SQL statement.
A SQL statement might have a mix of both.
A METHOD_OPT of FOR ALL INDEXED COLUMNS SIZE AUTO is an immediate red flag. This is never a good setting.
FOR ALL COLUMNS SIZE AUTO without table-level preferences (SET_TABLE_PREFS) is another red flag.
As an interim step, consider use FOR ALL COLUMNS SIZE REPEAT to lock in the current histogram usage.
The end goal should be to have table level preferences set for all tables.
This relies on knowing your data, your data distribution, your code, and knowing which histograms make sense (i.e. for skewed columns) – it will be far fewer than gathered by default.
For columns with significant numbers of distinct skew, it may be necessary to manually craft the histograms.
Volatile tables should have stats set to an appropriate setting to generate appropriate plans for all situations and then those stats should be locked.
Stats which are gathered at any point during the volatility cycle may be good or may be problematic.
Similarly dynamic sampling can only see the data at the time of hard parse – you might be lucky and this is better than stats which say 0 rows but it can be a time bomb.
Turn off optimizer features
Turning off optimizer features might be best done via a LOGON trigger and turning such off for a subset of application users. These features include:
- Bind Variable Peeking – off via _optim_peek_user_binds = false
- Cardinality Feedback and ACS – should be disabled by turning off bind variable peeking but off via _optimizer_use_feedback = false, _optimizer_adaptive_cursor_sharing = false, _optimizer_extended_cursor_sharing_rel = “none”
- Dynamic Sampling – optimizer_dynamic_sampling to 0
- 12c additional adaptive features – e.g. adaptive execution plans
Additionally it probably makes sense to turn off the adaptive direct path read behaviour or anything with the word adaptive or dynamic in it or associated to it
This functionality decides on whether to do full segment scans via the buffer cache or not and the behaviour is a runtime decision depending on the size of the object relative to the buffer cache AND depending on how much of the segment is currently in the cache.
- Adaptive direct path reads – _serial_direct_read = always
All too often I’ve seen a concurrently executed SQL statement switch to a “bad” plan involving a full table scan delivered via direct path reads stress out the IO subsystem because of the number of concurrent executions of that query which then affects performance across the DB.
The actions above are still not sufficient to guarantee plan stability but, for this goal above all else, this is likely to be the most appropriate action.
However, to further guarantee stability it is still likely that some degree of hinting – whether via manual hints, sql profiles or baselines – might be necessary for small numbers of SQL statements where the intial cost-based plan is not appropriate e.g. GTTs and other situations but it should be small number of statements.
SQL Execution Plan Instability – Summary & Opinion
The actions discussed above are made on the basis that we want to minimise the chance of execution plan instability at all costs.
By making this decision, we are prioritizing stability over all the features within Oracle designed to generate better plans for specific situations, sets of binds, etc.
Personally, I always recommend going with the default behaviour until such time as it causes a significant problem.
I also always recommend matching the scope of a solution to the scope of a problem.
For example, if we have a problem with one or two SQL statements, the potential solutions should be limited to those SQL statements.
We should never be making changes with a potential system-wide impact for the sake of a couple of SQL statements.
And even parameter changes can be injected to specific SQL statements either via a SQL Patch or via manual hints.
In my opinion, having multiple plans for a SQL statement is expected.
But what is under-appreciated is the extent to which this is normal.
These are normally only noticed when they cause a problem and the significant number of plans which regularly change plans without impact tend to go unnoticed.
It is also my opinion that SQL execution issues occur mostly when the SQL is badly written or when incompatible features are combined – e.g. peeked binds together with histograms – and I prefer to tackle that root cause rather than a generic concept of plan instability being inherently problematic.
A future path?
Might it be a good idea for there to be a built-in choice of optimizer “umbrella” modes – maximum performance vs maximum stability which would turn on/off some of the features above and/or adjust their sensitivity/aggressiveness?