Independent senior database engineering · Pittsburgh, PAsbajwa@bajwaconsulting.com

ORA-01555 snapshot too old: find it and fix it

A query needed an older copy of the data from UNDO, but that undo had already been overwritten. This free, read-only script shows when it happened, which SQL caused it and how much undo you need. Below it: the four fixes and the five SQL mistakes that make queries run long enough to hit it.

Script of the Week #7Instance tuningSQLOracle 12.2 · 19c · 21cRead-only
Quick answerORA-01555 means a query ran longer than Oracle kept the undo it needed. Find the SQL with V$UNDOSTAT.MAXQUERYID, make it finish sooner, and set UNDO_RETENTION above your longest query with enough undo space to keep it.
File
tuning/instance/undo_snapshot_too_old.sql
Run in
CDB$ROOT or non-CDB; on RAC, each instance
Run as
SYSTEM or a user with SELECT_CATALOG_ROLE
Usage
SQL> @undo_snapshot_too_old.sql
Changes
None (read-only). Fixes are printed, not run.
Licence
No extra pack needed (no ASH/AWR)

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 small

Oracle 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

  1. 1
    Undo settings and sizeundo_retention, undo tablespaces, autoextend, RETENTION GUARANTEE
  2. 2
    When ORA-01555 hitevery 10-minute interval in V$UNDOSTAT with an error or out-of-space
  3. 3
    Longest query vs undo keptplus a suggested undo_retention and the undo GB it needs
  4. 4
    The SQL behind itMAXQUERYID joined to V$SQLAREA, worst first
  5. 5
    Alert logORA-01555 messages with SQL_ID and query duration
  6. 6
    Long queries running nowflags any query already older than undo_retention
  7. 7
    Cartesian joinsSQL with MERGE JOIN CARTESIAN in its plan
  8. 8
    Stale statisticsapplication schemas with stale or missing stats
  9. 9
    Fixesprinted 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

ColumnMeaningExample
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.

✗ SLOW
SELECT o.order_id, c.name
FROM   orders o, customers c
WHERE  o.status = 'OPEN';
✓ FIXED
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.

✗ SLOW
SELECT l.*, o.order_date
FROM   order_lines l
JOIN   orders o
  ON   o.order_id = l.order_id;
✓ FIXED
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.

✗ SLOW
SELECT * FROM orders
WHERE  TRUNC(order_date) = DATE '2026-12-01';
✓ FIXED
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.

✗ SLOW
-- stats gathered months ago:
-- optimizer thinks ORDERS has 1,000 rows,
-- it really has 50 million
-- -> nested loops over 50M rows
✓ FIXED
EXEC DBMS_STATS.GATHER_TABLE_STATS(
       'SALES', 'ORDERS');

-- then re-check the plan

Section 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.

✗ SLOW
FOR r IN big_cursor LOOP
  UPDATE ...;
  COMMIT;
END LOOP;
✓ FIXED
FOR r IN big_cursor LOOP
  UPDATE ...;
END LOOP;
COMMIT;   -- once, or one set-based UPDATE

FAQ

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

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.