Oracle LOB

LOB (Large Object) is the Oracle data type for large text and binary data. A small value stays in the row; a large value is stored in a LOB segment separate from the table, and the row keeps only a LOB locator that points to the value's location. From 12c, newly created LOBs are stored as SecureFiles by default.

Key specs

ItemValue
Data typesCLOB (character), NCLOB (national character set), BLOB (binary), BFILE (points to a file outside the database, read-only)
Storage formatSecureFiles (default from 12c), BasicFiles (the older format)
Size kept in the rowUp to about 4,000 bytes (ENABLE STORAGE IN ROW, the default)
Maximum size(4GB - 1) × block size. About 32TB with 8KB blocks
CachingNOCACHE (default, reads and writes bypass the buffer cache), CACHE, CACHE READS
SecureFiles extrasCompression, deduplication, encryption (separately licensed options)
LONG, LONG RAWOlder data types. Not used for new work

How it works

Figure. In-row storage and LOB segment storage

flowchart LR
  A["Row: id, status, LOB column"] --> B{"Value is about 4,000 bytes or less"}
  B -- "Yes" --> C["Store the value in the row<br/>Table block"]
  B -- "No" --> D["The row holds a LOB locator"]
  D --> E["LOB index"]
  E --> F["Chunks in the LOB segment"]

Diagram explanation: A small value goes directly into the row in the Table block and is read through the buffer cache like any other column. A large value leaves only the LOB locator in the row, and the value is stored in chunks in the LOB segment. The LOB index lets Oracle find the chunk locations from the locator. The LOB segment can be placed in a different tablespace from the table.

The NOCACHE default. By default, an out-of-row LOB is read and written directly on disk without going through the buffer cache (the direct path read/write wait events). This keeps large values from occupying the buffer cache. If the same LOB is read often, switch to CACHE or CACHE READS.

The boundary with VARCHAR2. VARCHAR2 holds up to 4,000 bytes. If you raise the limit to 32,767 bytes with MAX_STRING_SIZE = EXTENDED, values over 4,000 bytes are stored out of row internally, like a LOB.

Working with LOBs. In SQL, read only part of a value with DBMS_LOB.SUBSTR, DBMS_LOB.GETLENGTH, DBMS_LOB.INSTR, and so on. A LOB column cannot be used in GROUP BY, ORDER BY, DISTINCT, or an ordinary B+Tree index. For text search, use an Oracle Text index.

Settings and checks

SELECT table_name, column_name, securefile, in_row, cache, chunk, segment_name
FROM user_lobs;

SELECT segment_name, segment_type, bytes / 1024 / 1024 AS mb
FROM user_segments WHERE segment_type LIKE 'LOB%';

CREATE TABLE docs (
  id   NUMBER PRIMARY KEY,
  body CLOB
) LOB (body) STORE AS SECUREFILE (ENABLE STORAGE IN ROW CACHE READS);

SELECT id, DBMS_LOB.GETLENGTH(body) AS len, DBMS_LOB.SUBSTR(body, 100, 1) AS head FROM docs;

Comparison with other DBMSs

OraclePostgreSQLInnoDBSQL Server
Separate data typeRequired (CLOB, BLOB)None. Applied automatically to text and byteaRequired (TEXT, BLOB) or a long VARCHARvarchar(max) and similar
Size kept in the rowAbout 4,000 bytesRows up to about 2KBRows up to about half a page8,000 bytes (can be turned off by a setting)
CachingBypasses the buffer cache by defaultSame as ordinary pagesSame as ordinary pagesSame as ordinary pages

In the articles

NOTE The default storage format and the limits can vary with the version and the COMPATIBLE setting.

Related pages