Large Usage Sequence Analysis (dseqbig.sql)
What This Script Does
Section titled “What This Script Does”This script identifies database sequences that have reached a high usage threshold, helping prevent sequence exhaustion issues before they occur. It analyzes sequence definitions and current values to find sequences approaching their maximum limits, which is critical for maintaining application availability.
Script
Section titled “Script”rem dseqbig.sqlremrem linesize = 80remttitle 'Large Usage Sequence Definitions'remcol sequence_owner format a12 heading 'OWNER'col sequence_name format a25 heading 'SEQUENCE NAME'col min_value format 9999 heading 'MIN|VALUE'col max_value format 999999999 heading 'MAX|VALUE'col increment_by format 999 heading 'INC'col cycle_flag format a1 heading 'C'col order_flag format a1 heading 'O'col cache_size format 9999 heading 'CACHE|SIZE'col last_number format 99999999 heading 'LAST|NUMBER'rembreak on sequence_ownerremselect sequence_owner, sequence_name, min_value, max_value, increment_by, cycle_flag, order_flag, cache_size, last_number from sys.dba_sequences where sequence_owner like upper('&owner') and sequence_name like upper('&sequence') and last_number >= &last_number order by sequence_owner, sequence_name;
-- Run the script in SQL*Plus or SQLcl@dseqbig.sql
-- When prompted, enter:-- owner: Schema owner pattern (% for all)-- sequence: Sequence name pattern (% for all)-- last_number: Minimum current value threshold (e.g., 1000000)
-- ExampleEnter value for owner: %Enter value for sequence: %Enter value for last_number: 1000000
Parameters
Section titled “Parameters”- &owner: Schema owner pattern (use % for all schemas)
- &sequence: Sequence name pattern (use % for all sequences)
- &last_number: Minimum current value to report (threshold)
Required Privileges
Section titled “Required Privileges”- SELECT on SYS.DBA_SEQUENCES
- Typically requires DBA role or SELECT_CATALOG_ROLE
Sample Output
Section titled “Sample Output”Large Usage Sequence Definitions
OWNER SEQUENCE NAME MIN MAX INC C O CACHE LAST VALUE VALUE SIZE NUMBER------------ ------------------------- ----- ----------- --- - - ----- --------SALES CUSTOMER_ID_SEQ 1 2147483647 1 N N 50 1456789SALES ORDER_ID_SEQ 1 9999999999 1 N N 100 2345678HR EMPLOYEE_ID_SEQ 100 9999999999 1 N N 20 1123456INVENTORY PRODUCT_ID_SEQ 1 2147483647 1 N N 25 1987654FINANCE TRANSACTION_ID_SEQ 1 9999999999 1 N N 200 5678901
Key Output Columns
Section titled “Key Output Columns”- OWNER: Schema that owns the sequence
- SEQUENCE NAME: Name of the sequence
- MIN VALUE: Minimum value the sequence can generate
- MAX VALUE: Maximum value the sequence can reach
- INC: Increment value for each NEXTVAL call
- C: Cycle flag (Y = cycles after reaching max, N = errors)
- O: Order flag (Y = guaranteed order in RAC, N = no guarantee)
- CACHE SIZE: Number of values cached in memory
- LAST NUMBER: Current highest value generated
Understanding Sequence Risk
Section titled “Understanding Sequence Risk”Critical Risk Indicators
Section titled “Critical Risk Indicators”- LAST NUMBER approaching MAX VALUE: Sequence exhaustion risk
- Non-cycling sequences (C = N): Will error when max reached
- High increment values: Faster approach to maximum
- Small cache sizes: Performance impact on high-usage sequences
Calculating Remaining Values
Section titled “Calculating Remaining Values”-- Approximate remaining valuesSELECT sequence_name, max_value - last_number as remaining_values, CASE WHEN max_value - last_number < 1000000 THEN 'CRITICAL' WHEN max_value - last_number < 10000000 THEN 'WARNING' ELSE 'OK' END as risk_levelFROM dba_sequencesWHERE last_number >= 1000000;
Performance Analysis
Section titled “Performance Analysis”High-Usage Sequence Characteristics
Section titled “High-Usage Sequence Characteristics”- Large LAST NUMBER values: Heavily used sequences
- Small CACHE SIZE with high usage: Potential performance bottleneck
- Order flag Y in RAC: Performance impact for globally ordered sequences
Optimization Opportunities
Section titled “Optimization Opportunities”- Increase cache size for frequently used sequences
- Consider sequence partitioning for extremely high-volume scenarios
- Review increment values for batch processing efficiency
Common Use Cases
Section titled “Common Use Cases”Proactive Monitoring
Section titled “Proactive Monitoring”-- Weekly check for sequences approaching limits@dseqbig.sql-- Enter high threshold like 100000000-- Review sequences nearing maximum values
Capacity Planning
Section titled “Capacity Planning”-- Identify high-growth sequences@dseqbig.sql-- Track growth rates over time-- Plan sequence modifications before exhaustion
Performance Analysis
Section titled “Performance Analysis”-- Find sequences with small caches and high usage@dseqbig.sql-- Look for performance optimization opportunities
Troubleshooting Sequence Issues
Section titled “Troubleshooting Sequence Issues”Sequence Approaching Maximum
Section titled “Sequence Approaching Maximum”-
Immediate Solutions:
-- Increase maximum valueALTER SEQUENCE schema.sequence_name MAXVALUE 99999999999;-- Enable cycling (if business logic allows)ALTER SEQUENCE schema.sequence_name CYCLE; -
Long-term Solutions:
-- Create new sequence with larger rangeCREATE SEQUENCE schema.new_sequence_nameSTART WITH 1MAXVALUE 999999999999999999999999999INCREMENT BY 1CACHE 100;
Performance Optimization
Section titled “Performance Optimization”-
Increase Cache Size:
-- Reduce contention in high-concurrency scenariosALTER SEQUENCE schema.sequence_name CACHE 1000; -
Remove Order Requirement (RAC environments):
-- Improve performance by removing global orderingALTER SEQUENCE schema.sequence_name NOORDER;
Sequence Exhaustion Recovery
Section titled “Sequence Exhaustion Recovery”-
Reset Sequence (if business allows):
-- Drop and recreate with new rangeDROP SEQUENCE schema.old_sequence;CREATE SEQUENCE schema.new_sequence START WITH 1; -
Application Changes:
- Modify application to handle larger sequence values
- Consider alternative ID generation strategies
- Implement sequence cycling logic if appropriate
Advanced Analysis
Section titled “Advanced Analysis”Growth Rate Calculation
Section titled “Growth Rate Calculation”-- Track sequence growth over timeSELECT sequence_name, last_number, SYSDATEFROM dba_sequencesWHERE sequence_name = 'YOUR_SEQUENCE';
-- Calculate daily growth rate-- (Run periodically and compare results)
Cross-Reference with Application Tables
Section titled “Cross-Reference with Application Tables”-- Find which tables use specific sequencesSELECT table_name, column_nameFROM dba_tab_columnsWHERE data_default LIKE '%YOUR_SEQUENCE%';
Related Scripts
Section titled “Related Scripts”- Sequence Definitions (dseq.sql) - Complete sequence analysis
- Object Count Analysis (objcnt.sql) - Database object statistics
- Schema Analysis (dobject.sql) - Object dependency analysis
- User Account Analysis (duser.sql) - Schema ownership verification