Oracle 11g比10g及以前 增加列,並帶預設值的新特性
轉載地址:
在11g以前,如果要在一個大表中增加一列,並設定預設值,那將是一個非常悲劇的事情.有些時候不得不選擇線上重定義功能來實現該需求.而在11g中增加新列並設定預設值,只是簡單的修改資料字典來實現該功能,大大提供效率
10g加列(預設值)
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
SQL> create table t_xifenfei
2 as select object_id,object_name from dba_objects;
Table created.
SQL> select count(*) from t_xifenfei;
COUNT(*)
----------
49827
SQL> desc t_xifenfei
Name Null? Type
----------------------------------------- -------- ----------------------------
OBJECT_ID NUMBER
OBJECT_NAME VARCHAR2(128)
SQL> set timing on
SQL> alter table t_xifenfei add c_xff varchar2(100) default '' not null;
Table altered.
Elapsed: 00:00:06.13
--使用了6秒鐘
SQL> select rowid,
2 dbms_rowid.rowid_relative_fno(rowid)rel_fno,
3 dbms_rowid.rowid_block_number(rowid)blockno,
4 dbms_rowid.rowid_row_number(rowid) rowno
5 from t_xifenfei where object_name='OBJ$';
ROWID REL_FNO BLOCKNO ROWNO
------------------ ---------- ---------- ----------
AAAMwJAAEAAAAB8AAr 4 124 43
SQL> alter system dump datafile 4 block 124;
System altered.
Elapsed: 00:00:00.08
11g增加列(預設值)
SQL> select * from v$version;
BANNER
-----------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
PL/SQL Release 11.2.0.3.0 - Production
CORE 11.2.0.3.0 Production
TNS for Linux: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production
SQL> create table t_xifenfei
2 as select object_id,object_name from dba_objects;
Table created.
SQL> select count(*) from t_xifenfei;
COUNT(*)
----------
74605
SQL> select rowid,
2 dbms_rowid.rowid_relative_fno(rowid)rel_fno,
3 dbms_rowid.rowid_block_number(rowid)blockno,
4 dbms_rowid.rowid_row_number(rowid) rowno
5 from t_xifenfei where object_name='OBJ$';
ROWID REL_FNO BLOCKNO ROWNO
------------------ ---------- ---------- ----------
AAASpRAAEAAAACrAAu 4 171 46
SQL> alter system dump datafile 4 block 171;
System altered.
SQL> set timing on
SQL> alter table t_xifenfei add c_xff varchar2(100) default '' not null;
Table altered.
Elapsed: 00:00:00.19
--只是使用了0.19秒
SQL> select rowid,
2 dbms_rowid.rowid_relative_fno(rowid)rel_fno,
3 dbms_rowid.rowid_block_number(rowid)blockno,
4 dbms_rowid.rowid_row_number(rowid) rowno
5 from t_xifenfei where object_name='OBJ$';
ROWID REL_FNO BLOCKNO ROWNO
------------------ ---------- ---------- ----------
AAASpRAAEAAAACrAAu 4 171 46
Elapsed: 00:00:00.04
SQL> alter system dump datafile 4 block 171;
System altered.
透過10g和11g的增加相同列和預設值的對比可以發現,11g的速度要比10g快很多很多,下面我們透過上面dump出來相關的資料塊來分析原因
dump分析
11g增加列之前dump
tab 0, row 0, @0x1f74
tl: 12 fb: --H-FL-- lb: 0x0 cc: 2
col 0: [ 2] c1 15
col 1: [ 5] 49 43 4f 4c 24
tab 0, row 1, @0x1f66
tl: 14 fb: --H-FL-- lb: 0x0 cc: 2
col 0: [ 2] c1 2f
col 1: [ 7] 49 5f 55 53 45 52 31
tab 0, row 2, @0x1f5b
tl: 11 fb: --H-FL-- lb: 0x0 cc: 2
col 0: [ 2] c1 1d
col 1: [ 4] 43 4f 4e 24
11g增加列之後dump
tab 0, row 0, @0x1f74
tl: 12 fb: --H-FL-- lb: 0x0 cc: 2
col 0: [ 2] c1 15
col 1: [ 5] 49 43 4f 4c 24
tab 0, row 1, @0x1f66
tl: 14 fb: --H-FL-- lb: 0x0 cc: 2
col 0: [ 2] c1 2f
col 1: [ 7] 49 5f 55 53 45 52 31
tab 0, row 2, @0x1f5b
tl: 11 fb: --H-FL-- lb: 0x0 cc: 2
col 0: [ 2] c1 1d
col 1: [ 4] 43 4f 4e 24
tab 0, row 3, @0x1f4f
tl: 12 fb: --H-FL-- lb: 0x0 cc: 2
col 0: [ 2] c1 10
col 1: [ 5] 55 4e 44 4f 24
10g增加列之後dump
因為10g在沒有增加列之前的dump和11g未增加列之前類似,所以未dump出來
tab 0, row 0, @0x1f63
tl: 29 fb: --H-FL-- lb: 0x2 cc: 3
col 0: [ 2] c1 15
col 1: [ 5] 49 43 4f 4c 24
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
tab 0, row 1, @0x1f44
tl: 31 fb: --H-FL-- lb: 0x2 cc: 3
col 0: [ 2] c1 2d
col 1: [ 7] 49 5f 55 53 45 52 31
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
tab 0, row 2, @0x1f28
tl: 28 fb: --H-FL-- lb: 0x2 cc: 3
col 0: [ 2] c1 1d
col 1: [ 4] 43 4f 4e 24
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
tab 0, row 3, @0x1f0b
tl: 29 fb: --H-FL-- lb: 0x2 cc: 3
col 0: [ 2] c1 10
col 1: [ 5] 55 4e 44 4f 24
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
對比發現11g在增加列之後,以前的資料dump出來的內容未有任何改變.也就是說:在10g中,我們增加一個列和預設值,會自動的增加到真實的資料中,而在11g中增加列和預設值並未真的加到11g的表中已經存在的資料中.
11g中插入新資料dump測試
SQL> insert into chf.t_xifenfei(object_id,object_name)
2 select object_id,object_name FROM DBA_OBJECTS;
74605 rows created.
SQL> commit;
Commit complete.
SQL> ALTER SYSTEM FLUSH BUFFER_CACHE;
System altered.
SQL> /
System altered.
--建議重新整理
SQL>
SQL>
SQL> select rowid,
2 dbms_rowid.rowid_relative_fno(rowid)rel_fno,
3 dbms_rowid.rowid_block_number(rowid)blockno,
4 dbms_rowid.rowid_row_number(rowid) rowno
5 from chf.t_xifenfei where object_name='OBJ$';
ROWID REL_FNO BLOCKNO ROWNO
------------------ ---------- ---------- ----------
AAASpRAAEAAAACrAAu 4 171 46
AAASpRAAEAAAB5TAAu 4 7763 46
SQL> ALTER SYSTEM DUMP DATAFILE 4 BLOCK 7763;
System altered.
--dump內容
tab 0, row 0, @0x4e3
tl: 29 fb: --H-FL-- lb: 0x1 cc: 3
col 0: [ 2] c1 15
col 1: [ 5] 49 43 4f 4c 24
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
tab 0, row 1, @0x500
tl: 31 fb: --H-FL-- lb: 0x1 cc: 3
col 0: [ 2] c1 2f
col 1: [ 7] 49 5f 55 53 45 52 31
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
tab 0, row 2, @0x51f
tl: 28 fb: --H-FL-- lb: 0x1 cc: 3
col 0: [ 2] c1 1d
col 1: [ 4] 43 4f 4e 24
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
tab 0, row 3, @0x53b
tl: 29 fb: --H-FL-- lb: 0x1 cc: 3
col 0: [ 2] c1 10
col 1: [ 5] 55 4e 44 4f 24
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
透過這裡看看出:在11g中後續插入的資料,預設值也插入到資料檔案中
在11g以前,如果要在一個大表中增加一列,並設定預設值,那將是一個非常悲劇的事情.有些時候不得不選擇線上重定義功能來實現該需求.而在11g中增加新列並設定預設值,只是簡單的修改資料字典來實現該功能,大大提供效率
10g加列(預設值)
SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod
PL/SQL Release 10.2.0.1.0 - Production
CORE 10.2.0.1.0 Production
TNS for Linux: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production
SQL> create table t_xifenfei
2 as select object_id,object_name from dba_objects;
Table created.
SQL> select count(*) from t_xifenfei;
COUNT(*)
----------
49827
SQL> desc t_xifenfei
Name Null? Type
----------------------------------------- -------- ----------------------------
OBJECT_ID NUMBER
OBJECT_NAME VARCHAR2(128)
SQL> set timing on
SQL> alter table t_xifenfei add c_xff varchar2(100) default '' not null;
Table altered.
Elapsed: 00:00:06.13
--使用了6秒鐘
SQL> select rowid,
2 dbms_rowid.rowid_relative_fno(rowid)rel_fno,
3 dbms_rowid.rowid_block_number(rowid)blockno,
4 dbms_rowid.rowid_row_number(rowid) rowno
5 from t_xifenfei where object_name='OBJ$';
ROWID REL_FNO BLOCKNO ROWNO
------------------ ---------- ---------- ----------
AAAMwJAAEAAAAB8AAr 4 124 43
SQL> alter system dump datafile 4 block 124;
System altered.
Elapsed: 00:00:00.08
11g增加列(預設值)
SQL> select * from v$version;
BANNER
-----------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - Production
PL/SQL Release 11.2.0.3.0 - Production
CORE 11.2.0.3.0 Production
TNS for Linux: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production
SQL> create table t_xifenfei
2 as select object_id,object_name from dba_objects;
Table created.
SQL> select count(*) from t_xifenfei;
COUNT(*)
----------
74605
SQL> select rowid,
2 dbms_rowid.rowid_relative_fno(rowid)rel_fno,
3 dbms_rowid.rowid_block_number(rowid)blockno,
4 dbms_rowid.rowid_row_number(rowid) rowno
5 from t_xifenfei where object_name='OBJ$';
ROWID REL_FNO BLOCKNO ROWNO
------------------ ---------- ---------- ----------
AAASpRAAEAAAACrAAu 4 171 46
SQL> alter system dump datafile 4 block 171;
System altered.
SQL> set timing on
SQL> alter table t_xifenfei add c_xff varchar2(100) default '' not null;
Table altered.
Elapsed: 00:00:00.19
--只是使用了0.19秒
SQL> select rowid,
2 dbms_rowid.rowid_relative_fno(rowid)rel_fno,
3 dbms_rowid.rowid_block_number(rowid)blockno,
4 dbms_rowid.rowid_row_number(rowid) rowno
5 from t_xifenfei where object_name='OBJ$';
ROWID REL_FNO BLOCKNO ROWNO
------------------ ---------- ---------- ----------
AAASpRAAEAAAACrAAu 4 171 46
Elapsed: 00:00:00.04
SQL> alter system dump datafile 4 block 171;
System altered.
透過10g和11g的增加相同列和預設值的對比可以發現,11g的速度要比10g快很多很多,下面我們透過上面dump出來相關的資料塊來分析原因
dump分析
11g增加列之前dump
tab 0, row 0, @0x1f74
tl: 12 fb: --H-FL-- lb: 0x0 cc: 2
col 0: [ 2] c1 15
col 1: [ 5] 49 43 4f 4c 24
tab 0, row 1, @0x1f66
tl: 14 fb: --H-FL-- lb: 0x0 cc: 2
col 0: [ 2] c1 2f
col 1: [ 7] 49 5f 55 53 45 52 31
tab 0, row 2, @0x1f5b
tl: 11 fb: --H-FL-- lb: 0x0 cc: 2
col 0: [ 2] c1 1d
col 1: [ 4] 43 4f 4e 24
11g增加列之後dump
tab 0, row 0, @0x1f74
tl: 12 fb: --H-FL-- lb: 0x0 cc: 2
col 0: [ 2] c1 15
col 1: [ 5] 49 43 4f 4c 24
tab 0, row 1, @0x1f66
tl: 14 fb: --H-FL-- lb: 0x0 cc: 2
col 0: [ 2] c1 2f
col 1: [ 7] 49 5f 55 53 45 52 31
tab 0, row 2, @0x1f5b
tl: 11 fb: --H-FL-- lb: 0x0 cc: 2
col 0: [ 2] c1 1d
col 1: [ 4] 43 4f 4e 24
tab 0, row 3, @0x1f4f
tl: 12 fb: --H-FL-- lb: 0x0 cc: 2
col 0: [ 2] c1 10
col 1: [ 5] 55 4e 44 4f 24
10g增加列之後dump
因為10g在沒有增加列之前的dump和11g未增加列之前類似,所以未dump出來
tab 0, row 0, @0x1f63
tl: 29 fb: --H-FL-- lb: 0x2 cc: 3
col 0: [ 2] c1 15
col 1: [ 5] 49 43 4f 4c 24
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
tab 0, row 1, @0x1f44
tl: 31 fb: --H-FL-- lb: 0x2 cc: 3
col 0: [ 2] c1 2d
col 1: [ 7] 49 5f 55 53 45 52 31
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
tab 0, row 2, @0x1f28
tl: 28 fb: --H-FL-- lb: 0x2 cc: 3
col 0: [ 2] c1 1d
col 1: [ 4] 43 4f 4e 24
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
tab 0, row 3, @0x1f0b
tl: 29 fb: --H-FL-- lb: 0x2 cc: 3
col 0: [ 2] c1 10
col 1: [ 5] 55 4e 44 4f 24
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
對比發現11g在增加列之後,以前的資料dump出來的內容未有任何改變.也就是說:在10g中,我們增加一個列和預設值,會自動的增加到真實的資料中,而在11g中增加列和預設值並未真的加到11g的表中已經存在的資料中.
11g中插入新資料dump測試
SQL> insert into chf.t_xifenfei(object_id,object_name)
2 select object_id,object_name FROM DBA_OBJECTS;
74605 rows created.
SQL> commit;
Commit complete.
SQL> ALTER SYSTEM FLUSH BUFFER_CACHE;
System altered.
SQL> /
System altered.
--建議重新整理
SQL>
SQL>
SQL> select rowid,
2 dbms_rowid.rowid_relative_fno(rowid)rel_fno,
3 dbms_rowid.rowid_block_number(rowid)blockno,
4 dbms_rowid.rowid_row_number(rowid) rowno
5 from chf.t_xifenfei where object_name='OBJ$';
ROWID REL_FNO BLOCKNO ROWNO
------------------ ---------- ---------- ----------
AAASpRAAEAAAACrAAu 4 171 46
AAASpRAAEAAAB5TAAu 4 7763 46
SQL> ALTER SYSTEM DUMP DATAFILE 4 BLOCK 7763;
System altered.
--dump內容
tab 0, row 0, @0x4e3
tl: 29 fb: --H-FL-- lb: 0x1 cc: 3
col 0: [ 2] c1 15
col 1: [ 5] 49 43 4f 4c 24
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
tab 0, row 1, @0x500
tl: 31 fb: --H-FL-- lb: 0x1 cc: 3
col 0: [ 2] c1 2f
col 1: [ 7] 49 5f 55 53 45 52 31
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
tab 0, row 2, @0x51f
tl: 28 fb: --H-FL-- lb: 0x1 cc: 3
col 0: [ 2] c1 1d
col 1: [ 4] 43 4f 4e 24
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
tab 0, row 3, @0x53b
tl: 29 fb: --H-FL-- lb: 0x1 cc: 3
col 0: [ 2] c1 10
col 1: [ 5] 55 4e 44 4f 24
col 2: [16] 77 77 77 2e 78 69 66 65 6e 66 65 69 2e 63 6f 6d
透過這裡看看出:在11g中後續插入的資料,預設值也插入到資料檔案中
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28371090/viewspace-1100938/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- oracle增加欄位帶預設值Oracle
- Oracle 11g DG新特性--Automatic block repairOracleBloCAI
- 新特性:/dev/shm對Oracle 11g的影響devOracle
- oracle 10G特性之awrOracle 10g
- mysql datetime增加預設值MySql
- Oracle 11g 新特性:只讀表(Read-only)Oracle
- Oracle Linux 6.7 靜預設安裝Oracle 11gOracleLinux
- Oracle實現自增列(序列+觸發器實現、Identity Columns新特性、序列作為預設值)Oracle觸發器IDE
- oracle監聽檔案listener.ora for 10g/11gOracle
- Oracle 21c新特性預覽與日常管理相關的幾個新特性Oracle
- oracle 11g 新特性之動態繫結變數窺視(一)Oracle變數
- oracle 11g 新特性之動態繫結變數窺視(二)Oracle變數
- Oracle從10g升級到11g詳細步驟Oracle
- 【10g SQL新特性】q-quote使用SQL
- 【DATAGUARD】Oracle19c dataguard新特性及部署Oracle
- Oracle Database 23c Beta 釋出,增加 300 多個新特性OracleDatabase
- 查詢oracle欄位預設值Oracle
- Oracle 增加 修改 刪除 列Oracle
- lightdb新特性--相容oracle儲存過程的聯合陣列Oracle儲存過程陣列
- Xcode10.2中LLDB增加的新特性XCodeLLDB
- java8 新特性之預設方法Java
- Oracle 11g RAC SCAN ip的原理及配置Oracle
- Oracle 11g新特新--SQL Test Case BuilderOracleSQLUI
- [20201109]11.2.0.4增加欄位與預設值問題.txt
- [轉載] Java7中增加的新特性Java
- Oracle 23C Free下載安裝及新特性Oracle
- 大咖帶你解讀 PostgreSQL 15 新特性 | 直播預告SQL
- 新增一列並增加列欄位註釋
- 陣列鍵值統一增加字首陣列
- Recoil 預設值及資料級聯的使用
- Oracle 9i, 10g, and 11g RAC on Linux所需要的Hangcheck-Timer Module介紹OracleLinuxGC
- oracle 10g在linux下的安裝及簡單命令Oracle 10gLinux
- 2024.8.17及以前的總結
- SQL Server 2008資料複製新特性及其帶來的價值(下)XDSQLServer
- Oracle 11g的使用者密碼預設為180天過期的修改方法Oracle密碼
- [20210421]12c以上版本增加欄位與預設值.txt
- 預設建構函式和帶預設值的建構函式不能同時存在函式
- Swift 4.2 新特性預覽Swift
- Oracle merge 與 PG新特性 UPSERTOracle