Overview
Oracle uses a cost-based optimizer (CBO). It chooses the plan with the lowest estimated cost using object statistics (row counts, distinct values, histograms), system statistics and the SQL. General principles are in query-planning. The Oracle-specific highlights are automatic statistics gathering, histograms, bind variable peeking and adaptive cursor sharing.
Core concepts
- Gather with
DBMS_STATS(notANALYZE):DBMS_STATS.GATHER_TABLE_STATS(user, 'ORDERS'). An automatic maintenance job gathers stale statistics nightly; refresh manually after bulk loads. - Histograms capture skew. Oracle creates them automatically based on column usage (
METHOD_OPT => 'FOR ALL COLUMNS SIZE AUTO'): frequency, top-frequency, hybrid. - Bind variable peeking: at the first hard parse the optimizer uses the actual bind value to choose a plan, which is then shared by later executions with other values.
- Adaptive cursor sharing (ACS): for bind-sensitive statements on skewed data, Oracle watches execution behaviour and creates multiple child cursors with different plans, picking one by the bind value ranges. It fixes the classic “sniffing” problem (compare sqlserver-parameter-sniffing).
- Adaptive plans can switch join methods mid-execution; SQL plan baselines (SPM) pin accepted plans; dynamic statistics (sampling) cover missing statistics.
- Parameters that matter:
OPTIMIZER_MODE(ALL_ROWS),OPTIMIZER_FEATURES_ENABLE,CURSOR_SHARING(leaveEXACT).
Worked example
Table ORDERS with 500,000 rows. Customer 42 has 5,043 rows, customer 7 has 47. After GATHER_TABLE_STATS:
SELECT column_name, num_distinct, histogram, num_buckets
FROM user_tab_col_statistics WHERE table_name = 'ORDERS';
-- CUSTOMER_ID 10000 HYBRID 254 (histogram built automatically)
-- STATUS 3 NONE 1Adaptive cursor sharing in action: one statement run with bind values 42 and 7 alternately.
VARIABLE b NUMBER
EXEC :b := 42 -- run, then EXEC :b := 7 and run twice, repeat
SELECT /* acs */ MAX(pad) FROM orders WHERE customer_id = :b;
SELECT child_number, plan_hash_value, is_bind_sensitive bs, is_bind_aware ba, executions, buffer_gets
FROM v$sql WHERE sql_text LIKE 'select /* acs */%' ORDER BY child_number;| Child | Plan | bind sensitive | bind aware | Executions | Buffer gets |
|---|---|---|---|---|---|
| 0 | full table scan (630573765) | Y | N | 2 | 18,900 |
| 1 | index range scan (3552148657) | Y | Y | 3 | 150 (50 each) |
| 2 | full table scan | Y | Y | 1 | 9,443 |
The first plan (compiled for 42) was wrong for 7; after the optimizer noticed the difference it created a bind-aware child per value range, so value 7 now costs 50 buffer gets instead of 9,443. SQL Server needed OPTION (RECOMPILE) in the equivalent test.
Pitfalls and production notes
- Do not freeze statistics blindly; if you lock them, plans cannot adapt.
- Literals instead of binds mean a new parse per value; binds + ACS is the intended combination.
- Hard parses are expensive; watch
parse count (hard)and library cache contention. - Compare estimated and actual rows in plans (oracle-execution-plans); a mismatch is the sign of missing histograms, stale statistics or correlated columns (extended statistics).
- Use SQL plan baselines or SQL Profiles (Tuning Pack) for stable plans across upgrades (oracle-patching-upgrades).
Practice questions
- What problem does adaptive cursor sharing solve, and what does
IS_BIND_AWAREindicate? - Why can
GATHER_TABLE_STATSafter a bulk load change a query from fast to slow? - When would you create extended statistics?
References
- Oracle Database SQL Tuning Guide, Query Optimizer Concepts; Histograms; Adaptive Cursor Sharing
Related
oracle-execution-plans · oracle-indexes · sqlserver-parameter-sniffing · query-planning