How to Create an Index on a 1 TB Table in Oracle

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.

  1. Online parallel index creation so that it not affect other activities.
CREATE INDEX IDX_EMP_ID
ON EMPLOYEE (EMP_ID)
ONLINE
PARALLEL 8;

Or Also Use Nologging option: it will reduce generation of redo logs. If no replication is used like Dataguard.

CREATE INDEX IDX_CUSTOMER_ID
ON ORDERS(CUSTOMER_ID)
TABLESPACE INDEX_TBS
NOLOGGING
PARALLEL 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_gb
FROM v$temp_space_header
GROUP BY tablespace_name;

4. Create the index in a dedicated tablespace

CREATE INDEX IDX_EMP_ID
ON EMPLOYEE(EMP_ID)
TABLESPACE INDEX_TBS
ONLINE
PARALLEL 8;

5. Monitor index creation

SELECT sid,
serial#,
opname,
sofar,
totalwork,
ROUND(sofar/NULLIF(totalwork,0)*100,2) pct_complete,
elapsed_seconds,
time_remaining
FROM v$session_longops
WHERE totalwork > 0
AND sofar < totalwork;

This entry was posted in Oracle on by .
Unknown's avatar

About SandeepSingh

Hi, I am working in IT industry with having more than 15 year of experience, worked as an Oracle DBA with a Company and handling different databases like Oracle, SQL Server , DB2 etc Worked as a Development and Database Administrator.

Leave a Reply