Index DDL Generation (qindexddl.sql)
What This Script Does
Section titled “What This Script Does”Generates CREATE INDEX DDL statements using Oracle’s DBMS_METADATA package. The script allows filtering by owner, index name, table name, and tablespace, and produces clean DDL output suitable for recreating indexes in other environments.
The Script
Section titled “The Script”rem qindexddl.sqlremttitle 'Index DDL Generation'remset heading offset pagesize 0set long 100000remdefine Owner='&OWNER'define TABLESPACE_NAME='&TABLESPACE_NAME'define TABLE_NAME='&TABLE_NAME'define INDEX_NAME='&INDEX_NAME'rembegin dbms_metadata.set_transform_param ( DBMS_METADATA.SESSION_TRANSFORM, 'CONSTRAINTS_AS_ALTER', true ); dbms_metadata.set_transform_param ( DBMS_METADATA.SESSION_TRANSFORM, 'STORAGE', false ); dbms_metadata.set_transform_param ( DBMS_METADATA.SESSION_TRANSFORM, 'SQLTERMINATOR', TRUE );end;/remcolumn x format a80 WORD_WRAPPEDremselect dbms_metadata.get_ddl('INDEX',u.index_name,u.owner) x fromdba_indexes uwhere owner like nvl(upper('&owner'),'%')and index_NAME like nvl(upper('&index_NAME'),'%')and TABLE_NAME like nvl(upper('&TABLE_NAME'),'%')and TABLESPACE_NAME like nvl(upper('&TABLESPACE_NAME'),'%')/remundefine Ownerundefine TABLESPACE_NAMEundefine TABLE_NAMEundefine INDEX_NAME
-- Generate DDL for all indexes@qindexddl.sql-- Enter: [press enter for all owners]-- Enter: [press enter for all tablespaces]-- Enter: [press enter for all tables]-- Enter: [press enter for all indexes]
-- Generate DDL for specific schema@qindexddl.sql-- Enter: SCOTT (for specific owner)-- Enter: [press enter for all tablespaces]-- Enter: [press enter for all tables]-- Enter: [press enter for all indexes]
-- Generate DDL for specific table's indexes@qindexddl.sql-- Enter: HR (for specific owner)-- Enter: [press enter for all tablespaces]-- Enter: EMPLOYEES (for specific table)-- Enter: [press enter for all indexes]
-- Generate DDL for specific index@qindexddl.sql-- Enter: HR (for specific owner)-- Enter: [press enter for all tablespaces]-- Enter: [press enter for all tables]-- Enter: IDX_EMP_DEPT (for specific index)
Parameters
Section titled “Parameters”The script prompts for:
- OWNER: Schema name (% for all schemas)
- TABLESPACE_NAME: Tablespace name (% for all tablespaces)
- TABLE_NAME: Table name (% for all tables)
- INDEX_NAME: Index name (% for all indexes)
Required Privileges
Section titled “Required Privileges”SELECT ANY DICTIONARY-- OR --SELECT on DBA_INDEXESEXECUTE on DBMS_METADATA
Sample Output
Section titled “Sample Output”CREATE INDEX "HR"."IDX_EMPLOYEES_DEPT" ON "HR"."EMPLOYEES" ("DEPARTMENT_ID") PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS TABLESPACE "INDEXES" ;
CREATE UNIQUE INDEX "HR"."EMP_EMP_ID_PK" ON "HR"."EMPLOYEES" ("EMPLOYEE_ID") PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS TABLESPACE "INDEXES" ;
CREATE INDEX "HR"."EMP_DEPARTMENT_IX" ON "HR"."EMPLOYEES" ("DEPARTMENT_ID") PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS TABLESPACE "INDEXES" ;
CREATE INDEX "HR"."EMP_JOB_IX" ON "HR"."EMPLOYEES" ("JOB_ID") PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS TABLESPACE "INDEXES" ;
DBMS_METADATA Transform Parameters
Section titled “DBMS_METADATA Transform Parameters”The script configures several transform parameters:
CONSTRAINTS_AS_ALTER = TRUE
- Generates constraints as separate ALTER statements
- Separates index creation from constraint definition
STORAGE = FALSE
- Excludes storage clause details
- Produces cleaner, more portable DDL
SQLTERMINATOR = TRUE
- Adds semicolons to end of statements
- Makes output ready for execution
Common Use Cases
Section titled “Common Use Cases”Database Migration
-- Extract index DDL for migration@qindexddl.sql-- Enter: schema_name-- Save output for deployment in target environment
Environment Refresh
-- Generate DDL for environment synchronization@qindexddl.sql-- Compare indexes between environments-- Apply missing indexes to target
Index Recreation
-- Generate DDL before dropping indexes@qindexddl.sql-- Save DDL for recreation after maintenance-- Ensure consistent index structure
Documentation
-- Document current index structures@qindexddl.sql-- Maintain index inventory-- Track index changes over time
Performance Tuning
-- Extract DDL for index analysis@qindexddl.sql-- Modify index structures for testing-- Compare performance with different configurations
Output Customization
Section titled “Output Customization”Additional Transform Parameters
-- Include storage detailsdbms_metadata.set_transform_param(DBMS_METADATA.SESSION_TRANSFORM, 'STORAGE', true);
-- Exclude tablespace clausedbms_metadata.set_transform_param(DBMS_METADATA.SESSION_TRANSFORM, 'TABLESPACE', false);
-- Include segment attributesdbms_metadata.set_transform_param(DBMS_METADATA.SESSION_TRANSFORM, 'SEGMENT_ATTRIBUTES', true);
Format Settings
-- Adjust line width for outputset linesize 120
-- Set longer page size for large outputset pagesize 1000
-- Adjust long setting for very large DDLset long 1000000
Filtering Examples
Section titled “Filtering Examples”By Tablespace
-- Get indexes in specific tablespace@qindexddl.sql-- Enter: [all owners]-- Enter: INDEX_TBS (specific tablespace)
By Index Pattern
-- Get indexes matching pattern@qindexddl.sql-- Enter: [all owners]-- Enter: [all tablespaces]-- Enter: [all tables]-- Enter: IDX_% (indexes starting with IDX_)
By Table Pattern
-- Get indexes for tables matching pattern@qindexddl.sql-- Enter: [all owners]-- Enter: [all tablespaces]-- Enter: EMP% (tables starting with EMP)
Post-Processing
Section titled “Post-Processing”Save to File
-- Spool output to filespool index_ddl.sql@qindexddl.sqlspool off
Modify Output
-- Edit generated DDL as needed-- Change tablespace assignments-- Modify index attributes-- Add comments or headers
Limitations
Section titled “Limitations”Memory Requirements
- Large DDL statements require sufficient LONG setting
- Complex indexes may need increased memory allocation
Transform Limitations
- Some advanced index features may not be fully captured
- Function-based indexes require careful review
- Domain indexes may need special handling
Performance Impact
Section titled “Performance Impact”- Variable: Depends on number of indexes
- DBMS_METADATA Overhead: Metadata extraction can be resource-intensive
- Large Schemas: May take significant time for schemas with many indexes