Large Segment Analyzer (dsegbig.sql)
What This Script Does
Section titled “What This Script Does”This script identifies and analyzes large database segments that exceed a specified size threshold. It helps DBAs find space-consuming objects, plan storage capacity, identify candidates for partitioning or archiving, and monitor segment growth patterns across schemas and tablespaces.
Script
Section titled “Script”rem dsegbig.sqlremrem linesize = 97remset linesize 132remttitle 'Large Segment Definitions'remcol owner format a12 heading 'OWNER'col segment_name format a30 heading 'SEGMENT NAME'col segment_type format a8 heading 'SEGMENT|TYPE'col tablespace_name format a15 heading 'TABLESPACE'col bytes format 9,999,999 heading 'BYTES (K)'col blocks format 999999 heading 'BLOCKS'col extents format 999 heading 'EXTS'col max_extents format 9999 heading 'MAX|EXTS'rembreak on report on owner skip 1compute sum of bytes blocks extents on report ownerremselect owner, segment_name, segment_type, tablespace_name, bytes/1024 bytes, blocks, extents, max_extents from sys.dba_segments where owner like upper('&owner') and segment_type like upper('&type') and segment_name like upper('&object') and bytes >= &bytes order by owner, segment_name;remset linesize 80
-- Run the script in SQL*Plus or SQLcl@dsegbig.sql
-- When prompted, enter parameters:-- owner: Schema owner pattern (% for all)-- type: Segment type pattern (% for all)-- object: Object name pattern (% for all)-- bytes: Minimum size in bytes
-- Examples:Enter value for owner: %Enter value for type: TABLEEnter value for object: %Enter value for bytes: 104857600 -- 100MB
-- Find large indexes in HR schemaEnter value for owner: HREnter value for type: INDEXEnter value for object: %Enter value for bytes: 52428800 -- 50MB
Parameters
Section titled “Parameters”- &owner - Schema owner pattern (supports wildcards)
- &type - Segment type (TABLE, INDEX, LOB, etc.)
- &object - Object name pattern
- &bytes - Minimum size threshold in bytes
Required Privileges
Section titled “Required Privileges”- SELECT on DBA_SEGMENTS
- Typically requires DBA role
Sample Output
Section titled “Sample Output”Large Segment Definitions
SEGMENT BYTES (K) BLOCKS EXTS MAXOWNER SEGMENT NAME TYPE TABLESPACE EXTS------------ ------------------------ -------- --------------- ------------ ------- ---- -----HR EMPLOYEES TABLE USERS 2,048,576 262144 128 2048 EMPLOYEES_HISTORY TABLE USERS 5,242,880 655360 256 2048 EMP_SALARY_IDX INDEX USERS 524,288 65536 64 2048 EMP_NAME_IDX INDEX USERS 131,072 16384 16 2048 EMPLOYEE_DOCS LOB USERS 8,388,608 1048576 512 2048 ------------ ------- ----sum 16,335,424 2048000 976
SALES ORDERS TABLE SALES_DATA 10,485,760 1310720 640 4096 ORDERS_2023 TABLE SALES_DATA 12,582,912 1572864 768 4096 ORDER_ITEMS TABLE SALES_DATA 8,388,608 1048576 512 4096 ORDER_IDX INDEX SALES_DATA 2,097,152 262144 128 4096 ------------ ------- ----sum 33,554,432 4194304 2048 ============ ======= ====sum 49,889,856 6242304 3024
Key Output Columns
Section titled “Key Output Columns”- OWNER - Schema owning the segment
- SEGMENT NAME - Name of the segment
- SEGMENT TYPE - Type (TABLE, INDEX, LOB, etc.)
- TABLESPACE - Tablespace containing the segment
- BYTES (K) - Size in kilobytes
- BLOCKS - Number of database blocks
- EXTS - Number of extents
- MAX EXTS - Maximum extents allowed
Understanding Large Segments
Section titled “Understanding Large Segments”Segment Types
Section titled “Segment Types”- TABLE - Regular heap tables
- INDEX - B-tree or bitmap indexes
- LOB - Large object segments
- TABLE PARTITION - Partitioned table segments
- INDEX PARTITION - Partitioned index segments
Size Considerations
Section titled “Size Considerations”- Large segments impact backup times
- May cause space allocation issues
- Candidates for partitioning
- Affect query performance
Common Use Cases
Section titled “Common Use Cases”Find Top Space Consumers
Section titled “Find Top Space Consumers”SELECT owner, segment_name, segment_type, ROUND(bytes/1024/1024/1024, 2) size_gbFROM dba_segmentsWHERE bytes > 1073741824 -- 1GBORDER BY bytes DESCFETCH FIRST 20 ROWS ONLY;
Analyze Growth by Tablespace
Section titled “Analyze Growth by Tablespace”SELECT tablespace_name, COUNT(*) large_segments, ROUND(SUM(bytes)/1024/1024/1024, 2) total_gbFROM dba_segmentsWHERE bytes > 104857600 -- 100MBGROUP BY tablespace_nameORDER BY SUM(bytes) DESC;
Check Extent Usage
Section titled “Check Extent Usage”SELECT owner, segment_name, extents, max_extents, ROUND(extents/max_extents*100, 2) pct_usedFROM dba_segmentsWHERE bytes > 104857600AND extents/max_extents > 0.8ORDER BY extents/max_extents DESC;
Space Management
Section titled “Space Management”Segment Advisor Recommendations
Section titled “Segment Advisor Recommendations”-- Run segment advisor for large segmentsDECLARE task_name VARCHAR2(30) := 'LARGE_SEG_ADVICE';BEGIN DBMS_ADVISOR.CREATE_TASK( advisor_name => 'Segment Advisor', task_name => task_name );
DBMS_ADVISOR.CREATE_OBJECT( task_name => task_name, object_type => 'TABLE', attr1 => 'SCHEMA', attr2 => 'TABLE_NAME' );
DBMS_ADVISOR.EXECUTE_TASK(task_name);END;/
Shrink Operations
Section titled “Shrink Operations”-- Enable row movementALTER TABLE owner.table_name ENABLE ROW MOVEMENT;
-- Shrink spaceALTER TABLE owner.table_name SHRINK SPACE;
-- Shrink space cascade (includes indexes)ALTER TABLE owner.table_name SHRINK SPACE CASCADE;
Partitioning Candidates
Section titled “Partitioning Candidates”Identify Tables for Partitioning
Section titled “Identify Tables for Partitioning”-- Large tables with date columnsSELECT s.owner, s.segment_name, ROUND(s.bytes/1024/1024/1024, 2) size_gb, c.column_name date_columnFROM dba_segments s, dba_tab_columns cWHERE s.owner = c.ownerAND s.segment_name = c.table_nameAND s.segment_type = 'TABLE'AND s.bytes > 5368709120 -- 5GBAND c.data_type = 'DATE'ORDER BY s.bytes DESC;
Check Partition Feasibility
Section titled “Check Partition Feasibility”-- Tables with high DML activitySELECT s.owner, s.segment_name, ROUND(s.bytes/1024/1024/1024, 2) size_gb, m.inserts, m.updates, m.deletesFROM dba_segments s, dba_tab_modifications mWHERE s.owner = m.table_ownerAND s.segment_name = m.table_nameAND s.bytes > 1073741824ORDER BY s.bytes DESC;
Performance Impact
Section titled “Performance Impact”Buffer Cache Usage
Section titled “Buffer Cache Usage”-- Large segments in buffer cacheSELECT owner, object_name, object_type, COUNT(*) buffers, ROUND(COUNT(*)*8/1024, 2) size_mbFROM v$bh b, dba_objects oWHERE b.objd = o.data_object_idGROUP BY owner, object_name, object_typeHAVING COUNT(*) > 1000ORDER BY COUNT(*) DESC;
Full Table Scan Impact
Section titled “Full Table Scan Impact”-- Large tables with FTSSELECT p.object_owner, p.object_name, s.bytes/1024/1024 size_mb, p.operation, p.optionsFROM v$sql_plan p, dba_segments sWHERE p.object_owner = s.ownerAND p.object_name = s.segment_nameAND p.operation = 'TABLE ACCESS'AND p.options = 'FULL'AND s.bytes > 104857600GROUP BY p.object_owner, p.object_name, s.bytes, p.operation, p.options;
Best Practices
Section titled “Best Practices”Storage Management
Section titled “Storage Management”- Monitor segment growth trends
- Set appropriate initial and next extent sizes
- Use locally managed tablespaces
- Consider compression for large tables
Maintenance Planning
Section titled “Maintenance Planning”- Schedule shrink operations during low activity
- Plan partitioning for tables over 2GB
- Archive or purge old data regularly
- Monitor extent allocation
Related Scripts
Section titled “Related Scripts”- Table Storage Analysis (dtable.md) - Detailed table storage
- Maximum Shrink Analysis (../administration/maxshrink.md) - Space reclamation
- Database Space Report (../administration/ddbspace.md) - Overall space usage
Advanced Analysis
Section titled “Advanced Analysis”Growth Trending
Section titled “Growth Trending”-- Track segment growth over timeWITH seg_sizes AS ( SELECT segment_name, bytes/1024/1024 size_mb, SYSDATE capture_date FROM dba_segments WHERE owner = '&owner' AND bytes > 104857600)SELECT segment_name, size_mb, LAG(size_mb) OVER (ORDER BY segment_name) prev_size, size_mb - LAG(size_mb) OVER (ORDER BY segment_name) growth_mbFROM seg_sizesORDER BY growth_mb DESC NULLS LAST;
Compression Candidates
Section titled “Compression Candidates”-- Estimate compression benefitsSELECT owner, segment_name, ROUND(bytes/1024/1024, 2) current_mb, compression, compress_forFROM dba_segmentsWHERE segment_type = 'TABLE'AND bytes > 1073741824AND (compression = 'DISABLED' OR compression IS NULL)ORDER BY bytes DESC;