Generate and interpret Oracle execution plans to identify and fix SQL performance bottlenecks.
Execution plans reveal exactly how the optimizer will fetch rows—table access paths, join methods, and predicate filters. Reading them lets you spot full-table scans, missing indexes, or inefficient joins before they hurt production.
Use EXPLAIN PLAN FOR
to store the plan, then query DBMS_XPLAN.DISPLAY()
. With ALLSTATS LAST
you also see actual run-time statistics.
EXPLAIN PLAN FOR
SELECT *
FROM Orders o
JOIN OrderItems oi ON oi.order_id = o.id
JOIN Products p ON p.id = oi.product_id
WHERE o.order_date > SYSDATE - 30;
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY(format => 'TYPICAL'));
OPERATION shows the action (e.g., TABLE ACCESS). OPTIONS refines it (FULL vs INDEX). OBJECT_NAME lists the table or index. COST and CARDINALITY estimate work and rows. BYTES estimates data volume.
Full-table scans on large tables, high COST values, or nested-loop joins against big row counts signal trouble. Create or rewrite indexes, add selective predicates, or replace nested loops with hash joins.
TYPICAL
is concise for daily tuning. ALL
adds predicates. ALLSTATS LAST
injects actual run-time metrics when the statement is executed with GATHER_PLAN_STATISTICS
.
Gather fresh table statistics before tuning. Use bind variables to avoid skew. Compare estimated vs actual rows after running with SET AUTOTRACE TRACEONLY
or DBMS_XPLAN.DISPLAY_CURSOR()
.
Yes. Query V$SESSION_LONGOPS
or DBMS_XPLAN.DISPLAY_CURSOR(format => 'TYPICAL +PEEKED_BINDS')
using the SQL_ID in V$SQL
.
Don’t rely on raw COST alone; always check estimated rows. Don’t ignore filter predicates at deeper plan levels—they indicate where rows explode.
No. EXPLAIN PLAN shows the optimizer’s prediction before execution. Use DBMS_XPLAN.DISPLAY_CURSOR with ALLSTATS LAST to see what really happened.
Yes. Insert rows from DBMS_XPLAN.DISPLAY into a logging table, or enable SQL Plan Baselines to preserve good plans automatically.
You need SELECT_CATALOG_ROLE or explicit SELECT on PLAN_TABLE, V$SQL, and V$SQL_PLAN to view and analyze plans.