V$UNDOSTAT.MAXQUERYID, make it finish sooner, and set UNDO_RETENTION above your longest query with enough undo space to keep it.Watch the 2-minute video on
YouTube @Bajwaconsulting555
What ORA-01555 means
You run a long query and get:
ORA-01555: snapshot too old: rollback segment number 9 with name
"_SYSSMU9_1650826591$" too smallOracle gives every query a consistent view of the data as of the moment it started. When rows change during the query, Oracle rebuilds the old version from undo. Undo is only kept for a while (UNDO_RETENTION, and only if there is space). If your query runs longer than that and other sessions overwrite the undo it needs, the query fails with ORA-01555. Query runtime > undo kept → ORA-01555.
What the script checks
- 1Undo settings and sizeundo_retention, undo tablespaces, autoextend, RETENTION GUARANTEE
- 2When ORA-01555 hitevery 10-minute interval in V$UNDOSTAT with an error or out-of-space
- 3Longest query vs undo keptplus a suggested undo_retention and the undo GB it needs
- 4The SQL behind itMAXQUERYID joined to V$SQLAREA, worst first
- 5Alert logORA-01555 messages with SQL_ID and query duration
- 6Long queries running nowflags any query already older than undo_retention
- 7Cartesian joinsSQL with MERGE JOIN CARTESIAN in its plan
- 8Stale statisticsapplication schemas with stale or missing stats
- 9Fixesprinted for you to review, never run
The heart of it is this query on V$UNDOSTAT, which keeps undo statistics in 10-minute intervals for about the last four days:
SELECT TO_CHAR(begin_time, 'DD-MON HH24:MI') int_start,
maxquerylen, -- longest query (seconds)
tuned_undoretention, -- undo Oracle actually kept (seconds)
ssolderrcnt, -- ORA-01555 errors
nospaceerrcnt, -- undo ran out of space
maxqueryid -- SQL_ID of the longest query
FROM v$undostat
WHERE ssolderrcnt > 0 OR nospaceerrcnt > 0
ORDER BY begin_time DESC;How to read the output
| Column | Meaning | Example |
|---|---|---|
| Longest query s (MAXQUERYLEN) | How long the longest query ran in that interval. | 5,420 (90 min) |
| Undo kept s (TUNED_UNDORETENTION) | How far back Oracle could actually keep undo. | 2,700 (45 min) |
| ORA-01555 (SSOLDERRCNT) | Queries that failed with snapshot too old. | 3 |
| Out of space (NOSPACEERRCNT) | Times the undo tablespace was full. | should be 0 |
In this example the longest query ran 90 minutes but Oracle only kept 45 minutes of undo, so three queries failed. Retention is shorter than the longest query: fix it below.
How to fix ORA-01555
Fix 1Raise UNDO_RETENTION
Set it above your longest query. Section 3 of the script suggests a value: longest query + 20%.
ALTER SYSTEM SET undo_retention = 7200 SCOPE=BOTH SID='*';Fix 2Give undo room to keep it
Retention only holds if the undo tablespace has space for it. Section 3 also estimates the GB needed.
ALTER TABLESPACE undotbs1
ADD DATAFILE SIZE 10G AUTOEXTEND ON MAXSIZE 32G;Fix 3Guarantee retention, carefully
Oracle will never overwrite unexpired undo.
ALTER TABLESPACE undotbs1 RETENTION GUARANTEE;Caution: when undo fills up, DML fails with ORA-30036 instead. Size undo first.
Fix 4Find and fix the SQL causing it
V$UNDOSTAT.MAXQUERYID is the SQL_ID of the longest query in each interval. The alert log names it too: “ORA-01555 caused by SQL statement below (SQL ID: …, Query Duration=… sec)”.
SELECT u.maxqueryid AS sql_id,
MAX(u.maxquerylen) AS longest_s,
SUM(u.ssolderrcnt) AS ora_01555,
MAX(s.sql_text) AS sql_text
FROM v$undostat u
LEFT JOIN (SELECT sql_id, MAX(SUBSTR(sql_text,1,160)) sql_text
FROM v$sqlarea GROUP BY sql_id) s
ON s.sql_id = u.maxqueryid
WHERE u.maxqueryid IS NOT NULL
GROUP BY u.maxqueryid
ORDER BY 3 DESC, 2 DESC;Then check its plan: SELECT * FROM TABLE(DBMS_XPLAN.DISPLAY_CURSOR('<sql_id>'));
Why is that SQL so slow? 5 common culprits
ORA-01555 hits long queries. Raising undo only buys time; making the query finish sooner fixes it for good.
1. Cartesian product
Two tables in FROM with no join condition: every row matches every row. 1M orders × 100K customers = 100 billion rows.
SELECT o.order_id, c.name
FROM orders o, customers c
WHERE o.status = 'OPEN';SELECT o.order_id, c.name
FROM orders o
JOIN customers c
ON c.cust_id = o.cust_id
WHERE o.status = 'OPEN';Look for MERGE JOIN CARTESIAN in the plan (section 7 of the script finds them).
2. Missing join key
The key is (org_id, order_id) but the join uses only one column, so rows multiply.
SELECT l.*, o.order_date
FROM order_lines l
JOIN orders o
ON o.order_id = l.order_id;SELECT l.*, o.order_date
FROM order_lines l
JOIN orders o
ON o.order_id = l.order_id
AND o.org_id = l.org_id;Join on every column of the key. Duplicate rows in the result are the warning sign.
3. Bad SQL: the index can't be used
Wrapping an indexed column in a function turns an index lookup into a full table scan.
SELECT * FROM orders
WHERE TRUNC(order_date) = DATE '2026-12-01';SELECT * FROM orders
WHERE order_date >= DATE '2026-12-01'
AND order_date < DATE '2026-12-02';Implicit conversions do the same, e.g. a VARCHAR2 column compared to a number.
4. Stale optimizer statistics
Wrong row estimates make the optimizer choose a slow plan.
-- stats gathered months ago:
-- optimizer thinks ORDERS has 1,000 rows,
-- it really has 50 million
-- -> nested loops over 50M rowsEXEC DBMS_STATS.GATHER_TABLE_STATS(
'SALES', 'ORDERS');
-- then re-check the planSection 8 lists schemas with stale stats; stats_health.sql gives the detail.
5. COMMIT inside a cursor loop
“Fetch across commit”: the loop's own commits free the undo its open cursor still needs.
FOR r IN big_cursor LOOP
UPDATE ...;
COMMIT;
END LOOP;FOR r IN big_cursor LOOP
UPDATE ...;
END LOOP;
COMMIT; -- once, or one set-based UPDATEFAQ
What causes ORA-01555 snapshot too old?
A query that runs longer than Oracle keeps the undo it needs. Oracle builds a read-consistent view of each block from undo as of the moment the query started; if that undo has been overwritten, the query fails with ORA-01555.
How do I find the SQL that caused ORA-01555?
Look at V$UNDOSTAT.MAXQUERYID, which holds the SQL_ID of the longest query in each 10-minute interval, and at the alert log, which records “ORA-01555 caused by SQL statement below” with the SQL ID and query duration.
What should UNDO_RETENTION be set to?
Longer than your longest query (MAXQUERYLEN in V$UNDOSTAT), with a margin. The undo tablespace must also be big enough to hold that much undo, or Oracle cannot honour the setting.
Should I use RETENTION GUARANTEE?
Only with care. It stops ORA-01555 for queries within the retention period, but when undo is full, DML fails with ORA-30036 instead. Size the undo tablespace first.
Is the script safe to run in production?
It is read-only: it only queries dictionary and V$ views and prints the fixes without running them. As always, try it in a non-production database first.
Related scripts
stats_health.sqlStale, missing and locked statistics for a schema.sql_details.sqlEverything about one SQL_ID, including its plan.sql_plan_history.sqlPlan changes for one SQL_ID over AWR history.alert_log_errors.sqlAlert log errors for the last N hours, by ORA- code.Always test in a non-production database first. More free scripts: DBA scripts library.
ORA-01555 keeps coming back?
I tune undo, long-running SQL and batch jobs on Oracle 19c/21c, RAC and Exadata. New DBA script every week on YouTube.