We, as a DBA often create indexes in Oracle Databases. We come to know about the Index size once it is being actually created. However, did you know that there is a simplest way to determine the size of index well in advance before actually creating it. At least, I did not know that.
Let me elaborate that with a simple example. I have a table MYAPP.ORDER_RECORD with following structure and data volume.
---------- Table Structure ---------- SQL> desc myapp.order_record Name Null? Type ----------------------------------- -------- ------------------------ ORDER_ID NOT NULL NUMBER ORDER_DATE DATE ITEM_NAME VARCHAR2(20) ITEM_COUNT NUMBER --------- Number of Records --------- SQL> select count(*) from myapp.order_record; COUNT(*) ---------- 11500445
Now suppose, I would like to create an index on the columns ORDER_ID and ITEM_NAME. However, I am not sure if the resultant index would fit in my tablespace. What we can do here is, make use of the EXPLAIN PLAN on the CREATE INDEX statement to find out the space requirement.
-------------- Use EXPLAIN PLAN to find index size -------------- SQL> explain plan for 2 create index myapp.order_record_idx01 on myapp.order_record (ORDER_ID,ITEM_NAME) 3 tablespace APPDATA; Explained. SQL> select * from table(dbms_xplan.display); PLAN_TABLE_OUTPUT ---------------------------------------------------------------------------------------------------------------- Plan hash value: 3190873030 ------------------------------------------------------------------------------------------------------------- | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time | Pstart| Pstop | ------------------------------------------------------------------------------------------------------------- | 0 | CREATE INDEX STATEMENT | | 11M| 197M| 22802 (1)| 00:00:01 | | | | 1 | INDEX BUILD NON UNIQUE| ORDER_RECORD_IDX01 | | | | | | | | 2 | SORT CREATE INDEX | | 11M| 197M| | | | | | 3 | PARTITION RANGE ALL | | 11M| 197M| 14956 (2)| 00:00:01 | 1 | 6 | | 4 | TABLE ACCESS FULL | ORDER_RECORD | 11M| 197M| 14956 (2)| 00:00:01 | 1 | 6 | ------------------------------------------------------------------------------------------------------------- Note ----- - estimated index size: 377M bytes 15 rows selected.
Can you observe the “Note” in the EXPLAIN PLAN output. Its says – estimated index size: 377M bytes.
Lets create the index assuming that I have 377M space available on my tablespace.
SQL> create index myapp.order_record_idx01 on myapp.order_record (ORDER_ID,ITEM_NAME) tablespace APPDATA; Index created.
Lets check the space occupied by this index.
SQL> select sum(bytes)/1024/1024 Size_MB from dba_segments where owner='MYAPP' and segment_name='ORDER_RECORD_IDX01'; SIZE_MB ---------- 408
Not bad, 377 MB was the estimated size whereas the actual index size occupied by the index is 408 MB. Note that this deviation is purely dependent on the accuracy of the table statistics.
There are also certain restrictions and limitations around this estimations. Richard Foote has very nicely presented this behavior in this blog post.
Reference
https://richardfoote.wordpress.com/2014/04/24/estimate-index-size-with-explain-plan-i-cant-explain