Why Fast Queries Suddenly Slow Down — And What Causes Execution Plan Changes in Oracle
Have you ever run a SQL query that used to be lightning-fast… and now it crawls?
The likely culprit? The Execution Plan Changed.
As an Oracle DBA or developer, understanding why execution plans change is crucial for diagnosing and preventing performance regressions. Let’s walk through the common causes of plan changes, how they impact performance, and what you can do about them — explained simply, just like your favorite teacher would.
🔍 First, What Is an Execution Plan?
Think of an execution plan as Oracle’s game plan for retrieving your data. When you run a SQL query, Oracle’s optimizer decides how to access the data: which indexes to use, in what order to join tables, whether to do a full scan, and so on.
But here’s the catch — this plan can change. And when it does, it might not be for the better.
🌀 When and Why Do Execution Plans Change?
Let’s explore the most common scenarios, what causes them, and what you — as a DBA or developer — should do.
1️⃣ Statistics Change
📊 “Your data changed, so Oracle adapts its plan.”
Oracle relies on table and index statistics to estimate how much data a query will return. If these stats change significantly (e.g., table grows, data distribution changes), Oracle may choose a different access path.
- Common Cause: Running DBMS_STATS or auto-gather stats job.
- Example: A small table became large, so Oracle switches from an index scan to a full table scan.
👨🏫 Tip for DBAs:
After gathering stats, always validate execution plans for critical queries.
2️⃣ Schema Changes (Indexes, Partitions, Constraints)
🏗️ “You added or removed a structure — Oracle re-evaluates the path.”
Any DDL change can influence how Oracle thinks about a query.
- Common Changes: Creating or dropping indexes, adding partitions, adding foreign keys or not null constraints.
- Impact: Optimizer might prefer a new path because more options are now available.
👨🏫 Tip for DBAs:
After any schema-level DDL, review impacted queries using tools like SQL Tuning Advisor or manual plan comparisons.
3️⃣ Bind Peeking and Data Skew
🎯 “The first value matters more than you think.”
When a query uses bind variables, Oracle “peeks” at the actual bind value during the first hard parse. If the value is unusual (e.g., rare), Oracle may pick a plan that isn’t good for typical values.
- Example: Querying WHERE country = :1. The first run is with “Iceland” (few rows), Oracle chooses index access. Later you run it with “USA” (millions of rows) — and the same plan now performs poorly.
👨🏫 Tip for DBAs:
Watch out for data skew. Use SQL Plan Management or Adaptive Cursor Sharing if needed.
4️⃣ System or Optimizer Parameter Changes
⚙️ “Changed settings = changed behavior.”
Oracle’s optimizer behavior is influenced by many parameters like:
- optimizer_mode
- optimizer_features_enable
- _optimizer_use_feedback
Changes can come from patching, upgrades, or direct parameter edits.
👨🏫 Tip for DBAs:
After upgrades or patching, compare execution plans. Maintain test environments to spot surprises early.
5️⃣ Plan Aged Out of Shared Pool
⏳ “If it’s gone from memory, Oracle makes a new choice.”
Execution plans live in the shared pool. If Oracle needs memory and removes an old plan, it re-parses the query next time — and might pick a different path.
- Cause: Memory pressure, infrequent query usage.
- Risk: New plan might not match the old one’s performance.
👨🏫 Tip for DBAs:
Use tools like AWR, ASH, or DBMS_XPLAN.DISPLAY_CURSOR
to track plan_hash_value
. Consider using SQL Plan Baselines to “lock” good plans.
✅ What Can You Do to Prevent Bad Plan Changes?
🔒 Use SQL Plan Baselines
They help “lock in” a known-good plan, so Oracle doesn’t surprise you.
📈 Monitor Plan Stability
Regularly track plan_hash_value
changes for your top queries.
🧪 Test Before You Change
Always test performance after applying patches, running stats, or making schema changes.
📚 In Summary
Execution plans are not static — they evolve. That’s both a strength and a challenge. Knowing when and why plans change helps you:
- Prevent slowdowns
- Fix regressions quickly
- Be proactive, not reactive
Next time a user says, “Hey, this query used to be faster!” — you’ll know exactly where to look.
💬 Have a question about execution plans? Share your scenario — let’s troubleshoot together!