How can I estimate the size of an Oracle index?

You can estimate the size of an index by running an explain plan on the create index statement:

create table t as
  select rownum r 
  from   dual 
  connect by level <= 1000000;

explain plan for
  create index i on t (r);

select * 
from   table(dbms_xplan.display(null, null, 'BASIC +NOTE'));

PLAN_TABLE_OUTPUT                                                                                                                         
--------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1744693673                                                                                                                 

---------------------------------------                                                                                                     
| Id  | Operation              | Name |                                                                                                     
---------------------------------------                                                                                                     
|   0 | CREATE INDEX STATEMENT |      |                                                                                                     
|   1 |  INDEX BUILD NON UNIQUE| I    |                                                                                                     
|   2 |   SORT CREATE INDEX    |      |                                                                                                     
|   3 |    TABLE ACCESS FULL   | T    |                                                                                                     
---------------------------------------                                                                                                     

Note                                                                                                                                        
-----                                                                                                                                       
   - estimated index size: 4194K bytes    

Look at the "Note" section at the bottom: estimated index size: 4194K bytes


Starting from version 10gR2 you can use DBMS_SPACE.CREATE_INDEX_COST

DBMS_SPACE.CREATE_INDEX_COST (
   ddl             IN    VARCHAR2,
   used_bytes      OUT   NUMBER,
   alloc_bytes     OUT   NUMBER,
   plan_table      IN    VARCHAR2 DEFAULT NULL);

From the docs: "This procedure determines the cost of creating an index on an existing table. The input is the DDL statement that will be used to create the index. The procedure will output the storage required to create the index."

See https://docs.oracle.com/database/121/ARPLS/d_space.htm#ARPLS68101

Example (also at sqlfiddle):

DECLARE 
 ub NUMBER; 
 ab NUMBER; 
BEGIN 
 DBMS_SPACE.CREATE_INDEX_COST (
    ddl             => 'CREATE INDEX x_1 ON t1 (a,b,c) TABLESPACE users',
    used_bytes      => ub,
    alloc_bytes     => ab
   );
 DBMS_OUTPUT.PUT_LINE('Used MBytes: ' || ROUND(ub/1024/1024)); 
 DBMS_OUTPUT.PUT_LINE('Alloc MBytes: ' || ROUND(ab/1024/1024)); 
END; 
/ 

Output:

Used MBytes: 1
Alloc MBytes: 2

You can use these Oracle Capacity planning and Sizing Spreadsheets.

For something not quite as full-blown, if you just want back of the envelope type rough estimates for the index:

Calculate the average size of each of the columns that make up the index key and sum the columns plus one rowid and add 2 bytes for the index row header to get the average row size. Now add just a little to the pctfree value for the index to come up with an overhead factor, maybe 1.125 for pctfree of 10.

number of indexed table rows X avg row len X 1.125

Note - if the index contains nullable columns then every table row may not appear in the index. On a single column index where 90% of the columns are null only 10% would go into the index.

Compare estimate to tablespace extent allocation method and adjust final answer if necessary.

Also a larger overhead factor may be better as the index gets bigger since the more data indexed the more branch blocks necessary to support the index structure and the calculation really just figures for leaf blocks.

Tags:

Sql

Oracle