Reputation: 26149
I'm considering adding an index to an Oracle table, but I'd like to first estimate the size of the index after it has been built (I don't need a precise size - just an estimate.)
Supposing I have access to all of the meta-data about the table (number of rows, columns, column data types, etc) that I can execute any arbitrary Oracle SQL query to get additional data about the current state of the table, and I know what I would want the index definition to be, how can I estimate this size?
Upvotes: 13
Views: 20759
Reputation: 708
The below SQL query can be used to know the estimated index size occupied by a table in oracle.
SELECT (row_size_in_bytes * cnt_of_rows)/1000/1000/1000 index_size_in_GB
FROM
(
SELECT table_name ,
(sum (column_length) / 1048576) * 1000000 row_size_in_bytes
FROM user_ind_columns
WHERE table_name=UPPER('&Enter_Table_Name')
GROUP BY table_name
) A,
(SELECT count(1) cnt_of_rows FROM &Enter_Table_Name );
Upvotes: 0
Reputation: 2787
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
Upvotes: 3
Reputation: 9825
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
Upvotes: 13
Reputation: 625147
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.
Upvotes: 10