Monday, June 23, 2008

Create Index: ORA-01450: maximum key length (6398) exceeded

Today ,I think about the key length to create index on Oracle.

So I look for the information on Internet.

Error: ORA 1450

Text: maximum key length exceeded
-------------------------------------------------------------------------------

Cause: The combined length of all the columns specified in a CREATE INDEX statement exceeded the maximum index length.
The maximum index length varies by operating system. The total index length is computed as the sum of the width of all indexed columns plus the number of indexed columns.
Date fields have a length of 7, character fields have their defined length, and numeric fields have a length of 22.Numeric length = (precision/2) + 1. If negative, add +1.

Action: Select columns to be indexed so the total index length does not exceed
the maximum index length for the operating system.
See also your operating system-specific Oracle documentation.


Oracle Reference =>

>>>

How it is Calculated
------------------------------------------------------
When creating an Index, the total length of the index cannot exceed a certain value. This value depends primarily on the DB_BLOCK_SIZE. If an attempt is made to create an index larger than the Maximum value, an ORA-1450 is raised:

ORA-01450 maximum key length (758) exceeded ->(2K Block)
ORA-01450 maximum key length (1578) exceeded ->(4K block)
ORA-01450 maximum key length (3218) exceeded ->(8K Block)
ORA-01450 maximum key length (6498) exceeded ->(16K Block)

The number in parends is the maximum allowable length of the index key for that particular system.

So, how is this number calculated?

The maximum key size means:

The total index length + length of the key (2 Bytes) + ROWID (6 Bytes) + the length of the rowid (1 byte).

The total index length is computed as the sum of the width of all indexed columns plus the number of indexed columns. Date fields have a length of 7, character fields have their defined length, and numeric fields have a length of 22. Numeric length = (precision/2) + 1. If negative, add +1.

For Funtion-based indexes, we must calculate the length of the return type.

This index key size is limited by the value of db_block_size, because a key value may not span multiple blocks. In fact, it is required that any index block must contain at least TWO index entries per block.

Therefore, the maximum key length for an index will be less than half of the DB_BLOCK_SIZE. The Oracle 8i Administrator's Guide states that the maximum size of a single index entry is approximately one-half the data block size. However, when considering that we must also leave space in the block according to PCTFREE, INITRANS, and space for block overhead (Block Header, ROW Directory, Table Directory, etc) the actual space that can be used for the Index key is actually just over 1/3 of the DB_BLOCK_SIZE.


Using default values for these storage options, the maximum length for indexes is as follows for different block sizes:

DB_BLOCK_SIZE: Maximum Index Key Length:
============== =========================

2K (2048) 758 Bytes
4K (4096) 1578 Bytes
8K (8192) 3218 Bytes
16K (16384) 6498 Bytes


If you hit a maximum key length in an index according to the DB_BLOCK_SIZE, you may need to recreate the database with a larger block size. The other alternative is to limit the size of the index. This is slightly more difficult with a Function-based index, when the return type is a varchar or RAW.

To limit the size of a function-based index you should consider using the SUBSTR or SUBSTRB function, to limit the number of Characters or Bytes returned. For more information on SUBTR and SUBSTRB, refer to the Oracle8i SQL Reference Guide.

>>>

NOTE: In Oracle9i or above, the maximum key length is larger

Example: on Oracle 11G

SQL> create table temp01 (data1 varchar2(4000), data2 varchar2(4000));

Table created.

SQL>
SQL>
SQL> create index idx_temp01 on temp01(data1,data2);

create index idx_temp01 on temp01(data1,data2)
*
ERROR at line 1:
ORA-01450: maximum key length (6398) exceeded

SQL> show parameter db_block_size;

NAME TYPE VALUE

------------------------------------ ----------- -------

db_block_size integer 8192


Enjoy...

2 comments:

Marco Gralike said...

Did you try playing with the "blocksize" parameter in the "create tablespace" statement...

SQL> select dbms_metadata.get_ddl('TABLESPACE','INDEX01')
2 from dual;

DBMS_METADATA.GET_DDL('TABLESPACE','INDEX01')
----------------------------------------------------------------------------

CREATE TABLESPACE "INDEX01" DATAFILE
'/u01/oracle/oradata/ONTW/index01.dbf'
SIZE 262144000
AUTOEXTEND ON NEXT 10485760 MAXSIZE 32767M
LOGGING ONLINE PERMANENT
--> BLOCKSIZE 8192 <--
EXTENT MANAGEMENT LOCAL AUTOALLOCATE SEGMENT
SPACE MANAGEMENT AUTO

HunterX said...

thank you for your comment.

I tried to think about the key length when we create index.

anyway,

SQL> create tablespace TEST2 datafile '/.../test2.dbf' size 100M AUTOEXTEND ON NEXT 10M MAXSIZE unlimited LOGGING BLOCKSIZE 32k EXTENT MANAGEMENT LOCAL AUTOALLOCATE SEGMENT SPACE MANAGEMENT AUTO;

So,

SQL> create index idx_temp01 on temp01(data1,data2) tablespace test2;

Index created.