開發的同學建oracle 表時候,字段類型經常設置會設置成varchar2(4000), 因為varchar2是可變長度字符串,實際使用多少就占用多少,設置的長度,是說字段可達到的最大長度,但是在建索引的時候發現一個問題:
我們使用在線創建索引或在線重新編譯索引時會報出字段長度超長的問題,雖然這列上實際字段最大長度只有幾個字符。
下面的實驗數據:
SQL> create table aa(id number(10),name varchar2(4000)); Table created. SQL> create index idx_name on aa(name) online; Index created. SQL> insert into aa values(10,'afdsafsa'); 1 row created. SQL> commit; Commit complete. SQL> alter index idx_name rebuild; Index altered. SQL> alter index idx_name rebuild online; alter index idx—me rebuild online * ERROR at line 1: ORA-00972: identifier is too long SQL> drop index idx_name; Index dropped. SQL> create index idx_name on aa(name) online; create index idx_name on aa(name) online * ERROR at line 1: ORA-00604: error occurred at recursive SQL level 1 ORA-01450: maximum key length (3215) exceeded SQL> create index idx_name on aa(name); Index created. SQL> alter index idx_name rebuild; Index altered.
可以看出,不使用online創建索引和rebuild索引是沒有問題的,使用online時會報錯,字段長度不能超過3215個字符。
