oracle 11G 新增欄位調整效能

huzhichengforce發表於2015-03-13

要在公司的測試庫新增二個pflag,pflag2欄位,表的資料量為590W 。

按照以前的方式alter table ehr_healthrecord add (pflag numer default (0));

花費時間為16:15min
alter table  hzwsj.EHR_HEALTHRECORD add  (pflag2 number default(0) not null);

花費時間為407msecs 毫秒!!! 天壤之別!!!
反向把pflag2 設定為null 花時間為15:28 秒 突突。。。。。alter table hzwsj.EHR_HEALTHRECORD modify  pflag2 null;
ORACLE 裡面有一段介紹
Adding Table Columns
To add a column to an existing table, use the ALTER TABLE...ADD statement.
The following statement alters the hr.admin_emp table to add a new column named bonus:
ALTER TABLE hr.admin_emp
      ADD (bonus NUMBER (7,2));
If a new column is added to a table, the column is initially NULL unless you specify the DEFAULT clause. When you specify a default value, the database immediately updates each row with the default value. Note that this can take some time, and that during the update, there is an exclusive DML lock on the table. For some types of tables (for example, tables without LOB columns), if you specify both a NOT NULL constraint and a default value, the database can optimize the column add operation and greatly reduce the amount of time that the table is locked for DML.
如果要在一張已經存在的表中新增一個欄位,你沒有指定預設值的話他預設為NULL,當你指定了預設值的時候資料庫直接在每行上直接更新新增的值,這需要花上一段時間,並且會在這段時間內產生DML 鎖。對於一些型別表(例如沒有lob欄位的表) 如果你新增的欄位是not null 的並且有一個預設的初始值,資料庫能夠對新增列進行優化,減少大量資料庫被DML鎖的時間。
You can add a column with a NOT NULL constraint only if the table does not contain any rows, or you specify a default value.

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/26764973/viewspace-1458695/,如需轉載,請註明出處,否則將追究法律責任。

相關文章