LOB Segment Analysis (dlob.sql)
What This Script Does
Section titled “What This Script Does”This script analyzes LOB (Large Object) segments in the database, displaying storage characteristics, performance settings, and associated indexes. It helps DBAs understand LOB storage configuration, identify performance tuning opportunities, and manage space allocation for CLOB, BLOB, and NCLOB columns.
Script
Section titled “Script”-- OWNER NOT NULL VARCHAR2(30) -- TABLE_NAME NOT NULL VARCHAR2(30) -- COLUMN_NAME VARCHAR2(4000) -- SEGMENT_NAME NOT NULL VARCHAR2(30) -- INDEX_NAME NOT NULL VARCHAR2(30) -- CHUNK NUMBER -- PCTVERSION NOT NULL NUMBER -- CACHE VARCHAR2(10) -- LOGGING VARCHAR2(3) -- IN_ROW VARCHAR2(3)select OWNER, TABLE_NAME, COLUMN_NAME, SEGMENT_NAME, INDEX_NAME, CHUNK, PCTVERSION, CACHE, LOGGING, IN_ROWfrom dba_lobswhere owner like nvl(upper('&owner'),'%')and table_name like nvl(upper('&table'),'%')and segment_name like nvl(upper('&lob_segment'),'%')order by 1,2,3/
-- Run the script in SQL*Plus or SQLcl@dlob.sql
-- When prompted, enter parameters:-- owner: Schema owner (% for all)-- table: Table name pattern (% for all)-- lob_segment: LOB segment name pattern (% for all)
-- Examples:Enter value for owner: HREnter value for table: %Enter value for lob_segment: %
-- Find specific LOB segmentsEnter value for owner: APPEnter value for table: DOCUMENTSEnter value for lob_segment: SYS_LOB%
Parameters
Section titled “Parameters”- &owner - Schema owner (supports wildcards)
- &table - Table name pattern
- &lob_segment - LOB segment name pattern
Required Privileges
Section titled “Required Privileges”- SELECT on DBA_LOBS
- Typically requires DBA role or SELECT ANY DICTIONARY
Sample Output
Section titled “Sample Output”OWNER TABLE_NAME COLUMN_NAME SEGMENT_NAME INDEX_NAME CHUNK PCTVERSION CACHE LOG IN_ROW------ ------------- -------------- ---------------------- ---------------------- ----- ---------- ---------- --- ------HR EMPLOYEES RESUME SYS_LOB0000074213C00 SYS_IL0000074213C00 8192 10 NO YES YESHR DOCUMENTS PDF_CONTENT SYS_LOB0000074234C00 SYS_IL0000074234C00 16384 20 YES NO NOHR DOCUMENTS XML_DATA SYS_LOB0000074234C00 SYS_IL0000074234C00 16384 20 YES NO YESAPP ATTACHMENTS FILE_DATA LOB_ATTACHMENTS_FILE IDX_ATTACHMENTS_FILE 32768 10 NO YES NOAPP IMAGES IMAGE_BLOB SYS_LOB0000074456C00 SYS_IL0000074456C00 32768 5 YES NO NO
Key Output Columns
Section titled “Key Output Columns”- OWNER - Schema owning the table
- TABLE_NAME - Table containing the LOB column
- COLUMN_NAME - Name of the LOB column
- SEGMENT_NAME - LOB segment name (system or user-defined)
- INDEX_NAME - Associated LOB index name
- CHUNK - Chunk size in bytes (unit of LOB manipulation)
- PCTVERSION - Percentage of LOB storage for versioning
- CACHE - Whether LOB is cached in buffer cache
- LOGGING - Whether changes are logged
- IN_ROW - Whether small LOBs stored in-row
Understanding LOB Storage
Section titled “Understanding LOB Storage”Storage Parameters
Section titled “Storage Parameters”- CHUNK - Basic unit of LOB access (multiple of block size)
- PCTVERSION - Space reserved for read consistency
- IN_ROW - LOBs < 4000 bytes can be stored inline
Performance Settings
Section titled “Performance Settings”- CACHE - Improves read performance for frequently accessed LOBs
- LOGGING - Affects recovery and performance
- NOCACHE NOLOGGING - Fastest for bulk loads
Common Use Cases
Section titled “Common Use Cases”Find Large LOB Segments
Section titled “Find Large LOB Segments”SELECT l.owner, l.table_name, l.column_name, s.bytes/1024/1024 size_mbFROM dba_lobs l, dba_segments sWHERE l.segment_name = s.segment_nameAND l.owner = s.ownerAND s.bytes > 100*1024*1024 -- Over 100MBORDER BY s.bytes DESC;
Identify Non-Cached LOBs
Section titled “Identify Non-Cached LOBs”SELECT owner, table_name, column_name, segment_nameFROM dba_lobsWHERE cache = 'NO'AND owner NOT IN ('SYS','SYSTEM')ORDER BY owner, table_name;
Find LOBs with High Version Retention
Section titled “Find LOBs with High Version Retention”SELECT owner, table_name, column_name, pctversionFROM dba_lobsWHERE pctversion > 20ORDER BY pctversion DESC;
Performance Tuning
Section titled “Performance Tuning”Optimize Chunk Size
Section titled “Optimize Chunk Size”-- Check average LOB sizeSELECT AVG(DBMS_LOB.GETLENGTH(column_name))/1024 avg_size_kb, MAX(DBMS_LOB.GETLENGTH(column_name))/1024 max_size_kbFROM owner.table_name;
-- Chunk size should be close to average LOB size
Enable Caching for Small LOBs
Section titled “Enable Caching for Small LOBs”-- Modify LOB to enable cachingALTER TABLE owner.table_nameMODIFY LOB (column_name) (CACHE);
Reduce Version Retention
Section titled “Reduce Version Retention”-- Lower PCTVERSION for less read consistency needsALTER TABLE owner.table_nameMODIFY LOB (column_name) (PCTVERSION 5);
Space Management
Section titled “Space Management”LOB Segment Sizes
Section titled “LOB Segment Sizes”SELECT l.owner, l.table_name, l.column_name, s.segment_name, s.bytes/1024/1024 size_mb, s.extents, s.blocksFROM dba_lobs l, dba_segments sWHERE l.segment_name = s.segment_nameAND l.owner = s.ownerAND l.owner = '&owner'ORDER BY s.bytes DESC;
Shrink LOB Segments
Section titled “Shrink LOB Segments”-- Enable row movement firstALTER TABLE owner.table_name ENABLE ROW MOVEMENT;
-- Shrink LOB segmentALTER TABLE owner.table_nameMODIFY LOB (column_name) (SHRINK SPACE);
Best Practices
Section titled “Best Practices”Storage Configuration
Section titled “Storage Configuration”- Set appropriate CHUNK size based on average LOB size
- Use CACHE for frequently accessed small LOBs
- Consider SECUREFILE LOBs for better performance
- Use compression for large text/XML LOBs
Performance Optimization
Section titled “Performance Optimization”- Enable caching for LOBs < 32KB accessed frequently
- Use NOCACHE NOLOGGING for bulk loads
- Set PCTVERSION based on read consistency needs
- Consider partitioning tables with large LOBs
Migration to SecureFile LOBs
Section titled “Migration to SecureFile LOBs”Check BasicFile LOBs
Section titled “Check BasicFile LOBs”-- Find BasicFile LOBs (candidates for migration)SELECT owner, table_name, column_name, securefileFROM dba_lobsWHERE securefile = 'NO'AND owner NOT IN ('SYS','SYSTEM');
Benefits of SecureFile
Section titled “Benefits of SecureFile”- Compression and deduplication
- Encryption support
- Better space management
- Improved performance
Related Scripts
Section titled “Related Scripts”- Table Storage Analysis (dtable.md) - Table storage details
- Large Table Statistics (dstatbig.md) - Statistics for large objects
- Database Objects (dobject.md) - Object overview
Advanced Analysis
Section titled “Advanced Analysis”LOB Access Patterns
Section titled “LOB Access Patterns”-- Monitor LOB read/write activitySELECT segment_name, physical_reads, physical_writes, physical_reads_direct, physical_writes_directFROM v$segment_statisticsWHERE segment_name IN ( SELECT segment_name FROM dba_lobs WHERE owner = '&owner')AND statistic_name IN ('physical reads','physical writes');
LOB Fragmentation
Section titled “LOB Fragmentation”-- Check for fragmented LOB segmentsSELECT l.owner, l.table_name, l.column_name, s.extents, s.bytes/s.blocks/1024 avg_block_fill_kbFROM dba_lobs l, dba_segments sWHERE l.segment_name = s.segment_nameAND l.owner = s.ownerAND s.extents > 100ORDER BY s.extents DESC;