What is adaptive query optimization?
In the old days of Oracle, once the Query Optimizer picked a plan, it was committed to it—even if that plan turned out to be a disaster mid-execution. It was like a GPS that told you to turn left into a lake and refused to recalculate when you started sinking.
Adaptive Query Optimization, introduced in Oracle 12c and refined in 19c and 23c, changes that. It allows the database to "change its mind" and adjust its strategy based on real-time data as the query is actually running.
The Optimizer makes decisions based on statistics.
Without Adaptive features, Oracle would try to process those 10 million rows using a method meant for 10 rows (like a Nested Loop), causing the database to hang.
An Adaptive Plan allows the Optimizer to delay its final decision on a specific part of the plan until it sees the first few rows of data.
The Pivot Point: Oracle creates a "Pivot Point" in the execution plan—usually a choice between a Nested Loop (good for small data) and a Hash Join (good for large data).
The Switch: As the query starts, Oracle monitors the rows coming out of the first table.
Sometimes the "map" is just wrong. Oracle has two ways to fix its own statistics automatically:
Dynamic Statistics: If the Optimizer is unsure about a query (e.g., a complex WHERE clause with multiple predicates), it will pause for a few milliseconds and "sample" the data to get a better estimate before starting the full execution.
SQL Plan Directives: If a query runs and the "Actual" rows are wildly different from the "Estimated" rows, Oracle creates a "Directive."
If a query finishes and Oracle realizes it chose a terrible plan, it doesn't just forget about it. It stores the "Actual" execution data in the SQL Plan Baseline. The next time you run that exact query, Oracle will use the knowledge from the previous failure to pick a much better plan. It literally learns from its mistakes.
| Feature | Standard Optimization | Adaptive Optimization |
| Decision Time | Before execution starts. | During and after execution. |
| Flexibility | Rigid; stays the course. | Can "pivot" join methods mid-stream. |
| Learning | Relies on manual DBMS_STATS. | Learns from actual row counts. |
| Complexity | Simple but prone to errors. | Complex but highly resilient. |
You can see if a plan is adaptive by looking at the execution plan notes.
- this is an adaptive plan
You can also use the display_adaptive format in dbms_xplan:
SELECT * FROM TABLE(dbms_xplan.display_cursor(FORMAT => 'ADAPTIVE'));
In this view, Oracle will show you the "inactive" parts of the plan—the paths it could have taken but chose not to once it saw the real data.
While Adaptive Optimization is amazing, it can sometimes cause "Plan Instability" (queries changing speed unexpectedly). In Oracle 19c, most of these features are "On" by default, but you can control them using the parameter:
OPTIMIZER_ADAPTIVE_PLANS = TRUE/FALSE