Skip to main content

Reorg Script for Oracle Tables Within the Same Tablespace

 -- Ensure you're logged in as the appropriate user


-- 1. Define variables for schema and table names

DEFINE schema_name = 'YOUR_SCHEMA_NAME';

DEFINE table_name = 'YOUR_TABLE_NAME';


-- 2. Check the status and size of the table before reorganization

SELECT table_name, num_rows, blocks, empty_blocks, avg_row_len

FROM dba_tables

WHERE owner = '&schema_name'

AND table_name = '&table_name';


-- 3. Move the table within the same tablespace to reorganize it

-- No tablespace specified, it will remain in the same tablespace

ALTER TABLE &schema_name..&table_name MOVE;


-- 4. Rebuild indexes associated with the table

-- Get all indexes associated with the table

SELECT 'ALTER INDEX ' || index_name || ' REBUILD;' 

FROM dba_indexes 

WHERE owner = '&schema_name'

AND table_name = '&table_name';


-- 5. Gather statistics for the table after reorganization

EXEC DBMS_STATS.GATHER_TABLE_STATS('&schema_name', '&table_name');


-- 6. Check the table status and size after reorganization

SELECT table_name, num_rows, blocks, empty_blocks, avg_row_len

FROM dba_tables

WHERE owner = '&schema_name'

AND table_name = '&table_name';


-- 7. Optionally, shrink the table space after reorganization (use with caution)

ALTER TABLE &schema_name..&table_name SHRINK SPACE CASCADE;


-- 8. If partitioned, handle partitions separately

-- ALTER TABLE &schema_name..&table_name MOVE PARTITION partition_name;


Comments

Popular posts from this blog

ORA-39014: One or more workers have prematurely exited.ORA-00018: maximum number of sessions exceeded

ERROR: I was Performing a full database import and during the import I faced the below error. ORA-39014: One or more workers have prematurely exited. ORA-39029: worker 6 with process name "DW07" prematurely terminated ORA-31672: Worker process DW07 died unexpectedly. Job "SYSTEM"."SYS_IMPORT_FULL_04" stopped due to fatal error at 00:59:40 ORA-39014: One or more workers have prematurely exited. SOLUTION:  Run the import with fewer parallel processes, like PARALLEL=2 instead of 8. I was able to run the import successfully. NOTE 1: This errors occurs when there are less session allocation in the database. check the session,process parameters and increase them accordingly. To avoid such errors again. NOTE 2 : Note: Increasing processes parameter increases the amount of shared memory that needs to be reserved & the OS must be configured to support the larger amount of shared memory. So here we first need to increase the Memory & SG

ORA-01143: cannot disable media recovery - file 1 needs media recovery

I got a request from the client - To flashback the database to the existing restore point & disable flashback and archive log mode for database UATB. Here I came a cross error - ORA-01143. I followed the below steps. 1. SQL> select name from v$database; NAME ------------ UATB 2. SQL> SELECT NAME FROM V$RESTORE_POINT WHERE GUARANTEE_FLASHBACK_DATABASE='YES' ORDER BY TIME; NAME --------- UATB_COPY Here I'm going to restore the database to the above restore point. NOTE: The flashback database restore has to be done in MOUNT stage of the database. SQL> select name from v$database; NAME --------- UATB SQL> shut immediate; Database closed. Database dismounted. ORACLE instance shut down. SQL> startup mount ORACLE instance started. Total System Global Area  612368384 bytes Fixed Size                  1250428 bytes Variable Size             167775108 bytes Database Buffers          436207616 bytes Redo Buffers