How To Improve SQL Statements Performance: Using SQL Plan Baselines
Identifying the Slow Query
We have the following scenario: - Oracle 11.2.0.3 version single instance database - Performance issue caused by the following bad query: with initial explain plan :

As shown in the Plan output, a full table scan was used, resulting in excessive IO for this query. It seemed this query needed an index to reduce the IO. Therefore I have added two indexes on 'status' and 'prevobjectid' columns for the EMPLOYEES table, gathered table statistics and then checked again the explain plan. We will see now that due to index creation the DISPLAY_AWR program shows a newly generated explain plan with improved cost using an index range scan versus the full table scan used by the initial plan (Plan hash value: 2172072736).
Now we have obtained a new, better execution plan in AWR for the SQL statement, but our next question would be, “How can we make sure it will be the only plan picked by the Cost Based Optimizer for future executions”? The answer: “Create a SQL Tuning Set for the SQL, then create a new SQL Baseline from the STS so the Optimize will choose the preferred Execution Plan”. Each time a SQL statement is compiled, the optimizer first uses a cost-based search method to build a best-cost plan, then tries to find a matching plan in the SQL plan baseline.
If a match is found, the optimizer will proceed using this plan. Otherwise, it evaluates the cost of each accepted plan in the SQL plan baseline and selects the plan with the lowest cost.
Steps for loading SQL Plans
Here are the steps for loading SQL Plans into SPM using AWR by implementing SQL Baselines for the bad query.
Step 1: Set up a SQL Baseline using known-good plan, sourced from AWR snapshots. To do so, SQL Plan Management must be active and the easiest condition to checking optimizer_use_sql_plan_baselines which needs to be TRUE.
- Database, operating system, or hardware upgrades.
- Database, operating system, or hardware configuration changes.
- Database initialization parameter changes.
- Schema changes, such as adding indexes or materialized views.
- Refreshing optimizer statistics.
- Creating or changing SQL profiles.
Conclusion
As this blog post demonstrates, SQL Plan Management (SPM) allows database users to maintain stable yet optimal performance for a set of SQL statements and baselines seem to be a definite step in the right direction. Baselines can be captured from multiple sources, SPM allowing new plans to be used if they perform better than the baseline fact that could improve the overall application/system functionality.Oracle Database Consulting Services
Ready to optimize your Oracle Database for the future?
Share this
Share this
More resources
Learn more about Pythian by reading the following blogs and articles.
Simple and Quick Way to Get SQL_ID of Query in Oracle
The Easy Way of Finding Similar SQL Statements

Optimizing Reports and Automating Failure Monitoring Using PL/SQL and the RMAN Catalog
Ready to unlock value from your data?
With Pythian, you can accomplish your data transformation goals and more.