在11g之前添加一个not null的字段很慢。在11g之后就很快了。我们先做一个測试,然后探究下原理。

SQL> select * from v$version;

BANNER

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

Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi

PL/SQL Release 10.2.0.1.0 - Production

CORE    10.2.0.1.0      Production

TNS for 64-bit Windows: Version 10.2.0.1.0 - Production

NLSRTL Version 10.2.0.1.0 - Production



SQL> drop table test purge;

SQL> create table test as select * from dba_objects;

SQL> select count(*) from test;

  COUNT(*)

----------

    151203

SQL> set timing on

SQL> alter table test add col1 char(1000) DEFAULT 'LARGE COLUMN' not null;

已用时间:  00: 00: 09.48



SQL> SELECT SUM(BYTES)/1024/1024 ||'M' FROM user_segments WHERE segment_name = 'TEST';

SUM(BYTES)/1024/1024||'M'

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

200M



在11g下:


SQL> select * from v$version;

BANNER

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

Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production

PL/SQL Release 11.2.0.1.0 - Production

CORE    11.2.0.1.0      Production

TNS for Linux: Version 11.2.0.1.0 - Production

NLSRTL Version 11.2.0.1.0 - Production

SQL> drop table test purge;

SQL> create table test as select * from dba_objects;

SQL> insert into test select * from dba_objects;

SQL> commit;

SQL> select count(*) from test;

  COUNT(*)

----------

    149012

SQL> set timing on

SQL> alter table test add col1 char(1000) DEFAULT 'LARGE COLUMN' not null;

已用时间:  00: 00: 00.07

SQL> SELECT SUM(BYTES)/1024/1024 ||'M' FROM user_segments WHERE segment_name = 'TEST';

SUM(BYTES)/1024/1024||'M'

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

18M

探究原理:dump block能够发现,在10g下产生了非常多行迁移和行链接。

而在11g中就是一个值,在官方文档中说是Oracle通过在数据字典中记录DEFAULT值,避免了繁重的更新操作。

在dump block看看:

alter session set tracefile_identifier = 'gg_test';

select rowid,

       dbms_rowid.rowid_object(rowid) object_id,

       dbms_rowid.rowid_relative_fno(rowid) file_id,

       dbms_rowid.rowid_block_number(rowid) block_id,

       dbms_rowid.rowid_row_number(rowid) num

  from test where rownum <5;

alter system dump datafile 5 block 1465683;



10g下:

tl: 9 fb: --H----- lb: 0x2  cc: 0

nrid:  0x0181e36b.1

tab 0, row 2, @0x1b3a

tl: 1076 fb: --H-FL-- lb: 0x2  cc: 14

col  0: [ 3]  53 59 53

col  1: [ 4]  43 4f 4e 24

col  2: *NULL*

col  3: [ 2]  c1 1d

col  4: [ 2]  c1 1d

col  5: [ 5]  54 41 42 4c 45

col  6: [ 7]  78 69 08 1e 0e 33 19

col  7: [ 7]  78 69 08 1e 0f 31 37

col  8: [19]  32 30 30 35 2d 30 38 2d 33 30 3a 31 33 3a 35 30 3a 32 34

col  9: [ 5]  56 41 4c 49 44

col 10: [ 1]  4e

col 11: [ 1]  4e

col 12: [ 1]  4e

col 13: [1000]

 4c 41 52 47 45 20 43 4f 4c 55 4d 4e 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20

tab 0, row 3, @0x1b31

tl: 9 fb: --H----- lb: 0x2  cc: 0

nrid:  0x0181e36b.2  --nrid行迁移

tab 0, row 4, @0x1b28

tl: 9 fb: --H----- lb: 0x2  cc: 0

nrid:  0x0181e36b.3



11g下:

tab 0, row 0, @0x3f33

tl: 77 fb: --H-FL-- lb: 0x0  cc: 14

col  0: [ 3]  53 59 53

col  1: [ 5]  49 43 4f 4c 24

col  2: *NULL*

col  3: [ 2]  c1 15

col  4: [ 2]  c1 03

col  5: [ 5]  54 41 42 4c 45

col  6: [ 7]  78 72 06 09 0c 1a 2f

col  7: [ 7]  78 72 06 09 0c 26 33

col  8: [19]  32 30 31 34 2d 30 36 2d 30 39 3a 31 31 3a 32 35 3a 34 36

col  9: [ 5]  56 41 4c 49 44

col 10: [ 1]  4e

col 11: [ 1]  4e

col 12: [ 1]  4e

col 13: [ 2]  c1 02

tab 0, row 1, @0x3ee4

tl: 79 fb: --H-FL-- lb: 0x0  cc: 14

col  0: [ 3]  53 59 53

col  1: [ 7]  49 5f 55 53 45 52 31

col  2: *NULL*

col  3: [ 2]  c1 2f

col  4: [ 2]  c1 2f

col  5: [ 5]  49 4e 44 45 58

col  6: [ 7]  78 72 06 09 0c 1a 2f

col  7: [ 7]  78 72 06 09 0c 1a 2f

col  8: [19]  32 30 31 34 2d 30 36 2d 30 39 3a 31 31 3a 32 35 3a 34 36

col  9: [ 5]  56 41 4c 49 44

col 10: [ 1]  4e

col 11: [ 1]  4e

col 12: [ 1]  4e

col 13: [ 2]  c1 05

最新文章

  1. jasmine test 页面测试工具
  2. HttpUrlConnection 的 setDoOutput 与 setDoInput
  3. Codeforces 734E. Anton and Tree 搜索
  4. 谈谈我的编程之路---WAMP(一)
  5. 发现了一个很好的Pasical编程软件Lazarus
  6. unity, eulerAngle
  7. Unity3D 4.61 实现淡入淡出的场景过渡方法。
  8. Adobe Edge Animate –svg地图交互-精确的边缘及颜色置换
  9. META 标签的使用
  10. ubuntu的vi
  11. 【转】那些好用的iOS开发工具
  12. UIViewController加载过程
  13. EWS 通过SubscribeToPullNotifications订阅Exchange新邮件提醒
  14. JSONP 爬虫
  15. System.Web.Mvc 3.0.0.1 和 3.0.0.0 有什么区别?被 Microsoft ASP.NET MVC 的一次安全更新害惨了!!!
  16. 个人建站&amp;mac下安装hexo
  17. 微博第三方登录使用social_django实现显示登陆的用户名
  18. RGBColorspace 与 GRAYColorspace 图片混合后,生成的视频有点问题
  19. 【bzoj 1901】Zju2112 Dynamic Rankings
  20. react面试问题总结

热门文章

  1. 回文串(bzoj 3676)
  2. Python 函数的一般形式及参数
  3. python for循环及常用函数
  4. LeetCode OJ--Word Break II ***@
  5. win7下用PyInstaller把Python代码打包成exe文件
  6. IIS 7 Access to the path ‘c:\windows\system32\inetsrv\’ is denied
  7. java加载类的方法1.classloader 2.class.forName()
  8. smartsvn学习(-)
  9. 键盘弹出后上提view隐藏后下拉view还原并修改scroll过程中旋转屏幕到竖屏view显示错误
  10. iphone开发-SQLite数据库使用