Sequence Definitions (dseq.sql)
What This Script Does
Section titled “What This Script Does”This script provides a comprehensive view of database sequences and their configuration parameters. It displays sequence properties including value ranges, increment settings, caching behavior, and current usage levels, helping DBAs manage sequence objects and prevent exhaustion issues.
Script
Section titled “Script”rem dseq.sqlremrem linesize = 80remttitle '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') order by sequence_owner, sequence_name;
-- Run the script in SQL*Plus or SQLcl@dseq.sql
-- When prompted, enter:-- owner: Schema owner pattern (% for all)-- sequence: Sequence name pattern (% for all)
-- ExamplesEnter value for owner: %Enter value for sequence: %
-- Or filter specific sequencesEnter value for owner: SALESEnter value for sequence: %ORDER%
Parameters
Section titled “Parameters”- &owner: Schema owner pattern (use % for wildcard)
- &sequence: Sequence name pattern (use % for wildcard)
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”Sequence Definitions
OWNER SEQUENCE NAME MIN MAX INC C O CACHE LAST VALUE VALUE SIZE NUMBER------------ ------------------------- ----- --------- --- - - ----- --------HR EMPLOYEE_ID_SEQ 100 999999999 1 N N 20 123456 DEPARTMENT_ID_SEQ 1 999999999 1 N N 20 567
SALES CUSTOMER_ID_SEQ 1 999999999 1 N N 50 2345678 ORDER_ID_SEQ 1 999999999 1 N N 100 4567890 ORDER_LINE_SEQ 1 999999999 1 N N 25 789012
WAREHOUSE PRODUCT_ID_SEQ 1 999999999 1 N N 50 1234567 LOCATION_ID_SEQ 1 999999999 10 N Y 100 5670
Key Output Columns
Section titled “Key Output Columns”- OWNER: Schema that owns the sequence
- SEQUENCE NAME: Name of the sequence object
- 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 Configuration
Section titled “Understanding Sequence Configuration”Value Range Management
Section titled “Value Range Management”- MIN/MAX VALUE: Defines sequence boundaries
- LAST NUMBER: Shows current usage level
- Remaining values: MAX_VALUE - LAST_NUMBER
Performance Settings
Section titled “Performance Settings”- CACHE SIZE: Improves performance by pre-allocating values
- ORDER flag: Impacts RAC performance (Y = slower, N = faster)
- INCREMENT BY: Step size for each nextval
Behavior Options
Section titled “Behavior Options”- CYCLE flag: Determines behavior when reaching MAX_VALUE
- Y: Restarts from MIN_VALUE (useful for certain applications)
- N: Throws error when exhausted (typical for primary keys)
Sequence Analysis
Section titled “Sequence Analysis”Usage Monitoring
Section titled “Usage Monitoring”- High usage sequences: Large LAST_NUMBER values
- Approaching limits: LAST_NUMBER near MAX_VALUE
- Growth patterns: Track LAST_NUMBER changes over time
Performance Optimization
Section titled “Performance Optimization”- Cache sizing: Balance performance vs gap risk
- Order requirements: Only use ORDER when necessary in RAC
- Increment optimization: Consider larger increments for batch operations
Capacity Planning
Section titled “Capacity Planning”- Exhaustion prevention: Monitor sequences approaching MAX_VALUE
- Growth estimation: Calculate time to exhaustion based on usage
- Sequence migration: Plan for larger value ranges
Common Use Cases
Section titled “Common Use Cases”Sequence Inventory
Section titled “Sequence Inventory”-- List all sequences@dseq.sql-- Enter % for both parameters-- Review all sequence configurations
Usage Analysis
Section titled “Usage Analysis”-- Find high-usage sequences@dseq.sql-- Enter % for both parameters-- Look for sequences with high LAST_NUMBER values
Exhaustion Prevention
Section titled “Exhaustion Prevention”-- Check sequences approaching limits@dseq.sql-- Calculate remaining capacity-- Plan sequence modifications
Performance Review
Section titled “Performance Review”-- Analyze cache and order settings@dseq.sql-- Review CACHE_SIZE and ORDER_FLAG-- Optimize for performance
Sequence Management
Section titled “Sequence Management”Creating Sequences
Section titled “Creating Sequences”-- Standard sequence for primary keysCREATE SEQUENCE sales.order_id_seq START WITH 1 INCREMENT BY 1 MAXVALUE 999999999999999999999999999 CACHE 100 NOORDER NOCYCLE;
Modifying Sequences
Section titled “Modifying Sequences”-- Increase cache size for performanceALTER SEQUENCE sales.order_id_seq CACHE 500;
-- Increase maximum valueALTER SEQUENCE sales.order_id_seq MAXVALUE 99999999999999999;
-- Reset sequence (requires special handling)-- Cannot directly reset - must drop and recreate
Monitoring Usage
Section titled “Monitoring Usage”-- Track sequence usage over timeSELECT sequence_name, last_number, SYSDATEFROM dba_sequencesWHERE sequence_owner = 'SALES';
Troubleshooting Sequence Issues
Section titled “Troubleshooting Sequence Issues”Sequence Exhaustion
Section titled “Sequence Exhaustion”-
Immediate fix:
-- Increase maximum valueALTER SEQUENCE schema.sequence_nameMAXVALUE 999999999999999999999999999; -
Enable cycling (if appropriate):
ALTER SEQUENCE schema.sequence_name CYCLE; -
Reset sequence (requires drop/recreate):
-- Get current max value from tableSELECT MAX(id_column) FROM table_name;-- Drop and recreate sequenceDROP SEQUENCE schema.sequence_name;CREATE SEQUENCE schema.sequence_name START WITH new_value;
Performance Issues
Section titled “Performance Issues”-
Small cache causing contention:
-- Increase cache sizeALTER SEQUENCE schema.sequence_name CACHE 1000; -
RAC ordering overhead:
-- Remove ordering if not requiredALTER SEQUENCE schema.sequence_name NOORDER;
Gap Analysis
Section titled “Gap Analysis”- Check for sequence gaps:
-- Compare sequence value to table maxSELECT s.last_number, MAX(t.id_column) max_table_value,s.last_number - MAX(t.id_column) gapFROM dba_sequences s, table_name tWHERE s.sequence_name = 'SEQUENCE_NAME';
Advanced Analysis
Section titled “Advanced Analysis”Sequence Utilization Report
Section titled “Sequence Utilization Report”-- Calculate utilization percentageSELECT sequence_owner, sequence_name, last_number, max_value, ROUND((last_number/max_value)*100, 2) utilization_pct, max_value - last_number remaining_valuesFROM dba_sequencesWHERE max_value != 999999999999999999999999999ORDER BY utilization_pct DESC;
Performance Impact Assessment
Section titled “Performance Impact Assessment”-- Identify sequences with potential performance issuesSELECT sequence_owner, sequence_name, cache_size, order_flag, CASE WHEN cache_size < 20 THEN 'Small Cache' WHEN order_flag = 'Y' THEN 'RAC Ordering Overhead' ELSE 'OK' END performance_concernFROM dba_sequencesWHERE cache_size < 20 OR order_flag = 'Y';
Dependency Analysis
Section titled “Dependency Analysis”-- Find tables using sequences (approximate)SELECT table_name, column_name, data_defaultFROM dba_tab_columnsWHERE data_default LIKE '%.nextval'OR data_default LIKE '%NEXTVAL';
Best Practices
Section titled “Best Practices”Sequence Design
Section titled “Sequence Design”- Use large maximum values to prevent exhaustion
- Cache appropriately: 20-100 for OLTP, higher for batch
- Avoid ORDER in RAC unless specifically required
- Don’t cycle primary key sequences
Monitoring Strategy
Section titled “Monitoring Strategy”- Regular capacity checks: Monitor sequences approaching limits
- Performance monitoring: Track sequences with contention
- Growth analysis: Estimate future requirements
- Documentation: Maintain sequence usage documentation
Related Scripts
Section titled “Related Scripts”- Large Usage Sequences (dseqbig.sql) - Focus on high-usage sequences
- Object Count Analysis (../administration/objcnt.md) - Object inventory including sequences
- Schema Objects (dobject.sql) - Complete object analysis
- Table Analysis (dtable.sql) - Table definitions that may use sequences