码迷,mamicode.com
首页 > 数据库 > 详细

Oracle 11g新增not null的字段比10g快--新特性

时间:2017-05-09 21:54:10      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:window   class   lin   column   edit   数据字典   通过   mil   for   

   在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

Oracle 11g新增not null的字段比10g快--新特性

标签:window   class   lin   column   edit   数据字典   通过   mil   for   

原文地址:http://www.cnblogs.com/slgkaifa/p/6832652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!