Building an index on a 1 TB table requires careful planning because it can consume significant CPU, memory, disk I/O, temporary space, and potentially affect application performance.
using Oracle 19c, assuming your table is approximately 1 TB.
In Oracle, we can use parallel option.
- Online parallel index creation so that it not affect other activities.
CREATE INDEX IDX_EMP_IDON EMPLOYEE (EMP_ID)ONLINEPARALLEL 8;
Or Also Use Nologging option: it will reduce generation of redo logs. If no replication is used like Dataguard.
CREATE INDEX IDX_CUSTOMER_IDON ORDERS(CUSTOMER_ID)TABLESPACE INDEX_TBSNOLOGGINGPARALLEL 8;
2. Check available space before creating the index
SET SERVEROUTPUT ON;DECLARE l_used_bytes NUMBER; l_alloc_bytes NUMBER;BEGIN DBMS_SPACE.CREATE_INDEX_COST( ddl => 'CREATE INDEX IDX_EMP_ID ON EMPLOYEE(EMP_ID)', used_bytes => l_used_bytes, alloc_bytes => l_alloc_bytes ); DBMS_OUTPUT.PUT_LINE( 'Estimated used GB: ' || ROUND(l_used_bytes / POWER(1024,3),2) ); DBMS_OUTPUT.PUT_LINE( 'Estimated allocated GB: ' || ROUND(l_alloc_bytes / POWER(1024,3),2) );END;/
3. Check TEMP tablespace
Index creation may require significant temporary space for sorting.
SELECT tablespace_name, ROUND(SUM(bytes_used)/1024/1024/1024,2) used_gb, ROUND(SUM(bytes_free)/1024/1024/1024,2) free_gbFROM v$temp_space_headerGROUP BY tablespace_name;
4. Create the index in a dedicated tablespace
CREATE INDEX IDX_EMP_IDON EMPLOYEE(EMP_ID)TABLESPACE INDEX_TBSONLINEPARALLEL 8;
5. Monitor index creation
SELECT sid, serial#, opname, sofar, totalwork, ROUND(sofar/NULLIF(totalwork,0)*100,2) pct_complete, elapsed_seconds, time_remainingFROM v$session_longopsWHERE totalwork > 0AND sofar < totalwork;