Every performance engineer has a favorite kind of problem. Mine is the quiet one: no errors, no alerts, no crash. A batch job that used to finish before lunch is now finishing after dinner, and everyone swears nothing changed.
The Call
It started the way it always does. A report on the GTAS activity balances was running for close to an hour, and the business wanted to know why. The developers had checked the code. The DBAs had checked the stats. Everybody was confident, and everybody was stuck.
So I did what I always do first. I stopped reading opinions and started reading the row source statistics from the trace.
Reading the Plan from the Bottom Up
Here is the exact section of the trace I was staring at, with the index scan highlighted:
------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Starts | E-Rows | A-Rows | A-Time | Buffers | Reads |
------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | | 1 |00:56:47.12 | 58M | 55M |
| 1 | SORT AGGREGATE | | 1 | 1 | 1 |00:56:47.12 | 58M | 55M |
| *2 | TABLE ACCESS BY INDEX ROWID | FV_GTAS_ACTIVITY_BALANCES | 627 | 44 | 626 |00:56:46.89 | 58M | 55M |
| *3 | INDEX RANGE SCAN | FV_GTAS_ACTIVITY_BALANCES_N5 | 627 | 44 | 2280M |00:56:12.40 | 56M | 54M |
------------------------------------------------------------------------------------------------------------------------
The top of the plan told me the size of the crime. The SORT AGGREGATE had burned about 3,407 seconds—roughly 57 minutes—with around 58 million logical reads (Buffers) and 55 million physical reads (Reads). That is not a query that is slightly slow. That is a query hauling a mountain.
Almost all of that work belonged to one operation: the index range scan on FV_GTAS_ACTIVITY_BALANCES_N5. The rest of the plan was bystanders.
The Number That Didn't Make Sense
The index range scan reported 2,280,646,961 rows (2280M). That is 2.28 billion index entries.
The operation above it (TABLE ACCESS BY INDEX ROWID), which fetches the table row and applies the remaining filters, returned just 626 rows.
The plan started 627 times, so each start was visiting about 3.6 million index entries on average. And what had the optimizer expected for each start? The plan says E-Rows = 44 (card=44). Forty-four rows.
The optimizer was off by a factor of more than 80,000.
Why This Happens
The classic cause is an index that matches only part of the predicate—or has low selectivity on the leading/range columns relative to the actual filter conditions. The database walks billions of index entries, follows rowids into the table, evaluates remaining filter predicates, and throws nearly all of those rows away. Meanwhile, a bad cardinality estimate (card=44) makes a costly nested-loop or index-driven path look deceptively cheap.
What I Did Next
I pulled estimated versus actual rows (E-Rows vs. A-Rows), along with the access and filter predicates:
SELECT *
FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR(
sql_id => '&sql_id',
format => 'ALLSTATS LAST +PREDICATE'
));
The remediation hierarchy, in order of preference:
A better composite index aligned with the selective equality and range predicates.
Extended statistics (column groups or expressions) when correlated columns skew cardinality estimates.
A SQL Profile or SQL Patch when application code cannot be changed immediately and plan stability/directives are needed.
A SQL Plan Baseline to lock in a verified, efficient execution plan.
Before You Fix Anything: Who Is Already Steering This SQL?
Before creating any fix, I checked whether a profile, baseline, patch, or hint was already influencing statement fhbv5gf213vjq.
1. Anything Attached to This Cursor?
SELECT inst_id,
sql_id,
child_number,
plan_hash_value,
sql_profile,
sql_plan_baseline,
sql_patch,
REGEXP_SUBSTR(sql_fulltext, '/\*\+.*?\*/', 1, 1, 'n') AS embedded_hint
FROM gv$sql
WHERE sql_id = 'fhbv5gf213vjq';
2. Which Hints Did the Plan Use?
SELECT p.inst_id,
p.child_number,
p.id,
x.hint
FROM gv$sql_plan p,
XMLTABLE(
'//*[local-name()="hint"]'
PASSING XMLTYPE(p.other_xml)
COLUMNS hint VARCHAR2(4000) PATH '.'
) x
WHERE p.sql_id = 'fhbv5gf213vjq'
AND p.other_xml IS NOT NULL;
3. Which SQL Patches Exist?
SELECT name,
status,
category,
created
FROM dba_sql_patches;
4. What Do the Named Patches Say?
SELECT name,
status,
category,
force_matching,
signature,
created,
last_modified,
description,
sql_text
FROM dba_sql_patches
WHERE name IN (
'FVTEST1_PATCH2',
'FV_DACT_FIX_K_N2',
'XLA_FORCE_GENERIC_PLAN'
)
ORDER BY created DESC;
5. Which Cursors Are They Attached To?
SELECT p.name AS patch_name,
p.status AS patch_status,
p.force_matching,
s.inst_id,
s.sql_id,
s.child_number,
s.plan_hash_value,
s.sql_patch,
s.executions,
s.elapsed_time
FROM dba_sql_patches p
JOIN gv$sql s
ON (
(p.force_matching = 'NO' AND s.exact_matching_signature = p.signature)
OR (p.force_matching = 'YES' AND s.force_matching_signature = p.signature)
)
WHERE p.name IN (
'FVTEST1_PATCH2',
'FV_DACT_FIX_K_N2'
)
ORDER BY p.name, s.inst_id, s.sql_id, s.child_number;
Additional Queries for Deeper Understanding
When diagnosing a severe E-Rows vs. A-Rows mismatch and deciding between stats, indexes, or SQL patches, these extra queries pinpoint where the estimate went wrong:
A. Inspect Index Columns and Clustering Factor on FV_GTAS_ACTIVITY_BALANCES_N5
A high clustering factor close to the number of table rows means rowid probes will trigger heavy physical I/O:
SELECT ic.index_name,
ic.column_position,
ic.column_name,
ic.descend,
i.clustering_factor,
i.distinct_keys,
i.num_rows,
i.blevel,
i.leaf_blocks,
i.last_analyzed
FROM dba_ind_columns ic
JOIN dba_indexes i
ON i.owner = ic.index_owner
AND i.index_name = ic.index_name
WHERE ic.table_name = 'FV_GTAS_ACTIVITY_BALANCES'
AND ic.index_name = 'FV_GTAS_ACTIVITY_BALANCES_N5'
ORDER BY ic.column_position;
B. Check Column Cardinality, Nulls, and Histograms on the Table
Low num_distinct, skewed data without proper histograms, or stale stats often cause multi-million-row index scans:
SELECT column_name,
num_distinct,
num_nulls,
density,
histogram,
num_buckets,
sample_size,
last_analyzed
FROM dba_tab_col_statistics
WHERE table_name = 'FV_GTAS_ACTIVITY_BALANCES'
AND column_name IN (
SELECT column_name
FROM dba_ind_columns
WHERE table_name = 'FV_GTAS_ACTIVITY_BALANCES'
AND index_name = 'FV_GTAS_ACTIVITY_BALANCES_N5'
)
ORDER BY column_name;
C. Compare Plan History Across AWR Snapshots (DBA_HIST_SQLSTAT)
Confirm whether the statement degraded suddenly after a plan flip or grew gradually with data volume:
SELECT s.snap_id,
TO_CHAR(sn.begin_interval_time, 'YYYY-MM-DD HH24:MI') AS snap_start,
s.plan_hash_value,
s.executions_delta,
ROUND(s.elapsed_time_delta / 1e6 / NULLIF(s.executions_delta, 0), 2) AS avg_sec_per_exec,
ROUND(s.buffer_gets_delta / NULLIF(s.executions_delta, 0)) AS avg_lios_per_exec,
ROUND(s.disk_reads_delta / NULLIF(s.executions_delta, 0)) AS avg_pios_per_exec,
ROUND(s.rows_processed_delta / NULLIF(s.executions_delta, 0)) AS avg_rows_per_exec
FROM dba_hist_sqlstat s
JOIN dba_hist_snapshot sn
ON sn.snap_id = s.snap_id
AND sn.dbid = s.dbid
AND sn.instance_number = s.instance_number
WHERE s.sql_id = 'fhbv5gf213vjq'
ORDER BY s.snap_id DESC, s.plan_hash_value;
D. Verify Exact Matching vs. Force Matching Signatures for fhbv5gf213vjq
Useful when checking why a SQL patch or profile did or did not match a cursor:
Deploying the SQL Patch (FV_DACT_FIX_K_N2) to Target Environments
Once verified in lower environments, export the staging table FV_PATCH_STG from the source environment and deploy it to the target PDB using /nanubalu as the repository path:
Locate
DMPDIRin the target environment:SQLSELECT * FROM dba_directories WHERE directory_name = 'DMPDIR';Stage and set permissions on the dump file: Copy
fv_patch_stg.dmpfrom/nanubalu/EBSTS1/DPto theDMPDIRphysical location (/nanubalu/EBSPRD/DPfor PROD), then set permissions:Bashchmod 777 /nanubalu/EBSPRD/DP/fv_patch_stg.dmpImport the staging table into the target database:
Bashcd /nanubalu/EBSPRD/DP impdp \"/ as sysdba\" directory=DMPDIR tables=SYSTEM.FV_PATCH_STG dumpfile=fv_patch_stg.dmp logfile=fv_patch_stg_imp.log table_exists_action=REPLACELog into the target PDB(s) and confirm
SYSTEM.FV_PATCH_STG:SQLSELECT owner, table_name FROM dba_tables WHERE table_name = 'FV_PATCH_STG';Expected output:
PlaintextOWNER -------------------------------------------------------------------------------- TABLE_NAME -------------------------------------------------------------------------------- SYSTEM FV_PATCH_STGVerify row count:
SQLSELECT COUNT(*) FROM system.FV_PATCH_STG;COUNT(*) ---------- 1Unpack the SQL patch from the staging table:
SQLEXEC DBMS_SQLDIAG.UNPACK_STGTAB_SQLPATCH(patch_name => 'FV_DACT_FIX_K_N2', replace => TRUE, staging_table_name => 'FV_PATCH_STG', staging_schema_owner => 'SYSTEM');Verify the signature matches across environments: Compare
SIGNATUREagainst Defect and IT (for example,15180609084675737222should stay consistent across environments, whileCREATEDreflects the import timestamp):SQLCOL SIGNATURE FORMAT 999999999999999999999999999999999999999999 COL SQL_HANDLE FORMAT A30 COL CREATED FORMAT A30 SET LINES 200 SELECT SIGNATURE, SQL_HANDLE, CREATED FROM SYSTEM.FV_PATCH_STG;Plaintext
How to Remediate and Prevent Issues Like This in the Future
Turning a one-off firefighting win into lasting stability comes down to four habits:
Spot cardinality drift early: Always compare
E-RowstoA-RowswithDBMS_XPLAN.DISPLAY_CURSOR(..., format => 'ALLSTATS LAST +PREDICATE'). When a single step is off by orders of magnitude, fix the root cause rather than chasing symptoms.Align indexes with actual predicates: Make sure selective equality filters and range conditions match index column order, and watch for high
clustering_factoror heavy table rowid lookups after a broad index range scan.Keep statistics and extended stats accurate: Refresh optimizer statistics with appropriate sampling and create extended statistics (
DBMS_STATS.CREATE_EXTENDED_STATS) when correlated columns skew selectivity estimates.Promote patches to permanent fixes: Treat a SQL Patch, SQL Profile, or Baseline as a bridge—validate the runtime improvement under real load, then pursue the clean index or SQL/stats fix so future schema and data growth do not re-introduce the bottleneck.






No comments:
Post a Comment