- 最后登录
- 2023-8-16
- 在线时间
- 1686 小时
- 威望
- 2135
- 金钱
- 50532
- 注册时间
- 2011-10-12
- 阅读权限
- 200
- 帖子
- 5207
- 精华
- 39
- 积分
- 2135
- UID
- 2
|
4#
发表于 2012-5-9 14:52:32
single column b*tree index 不会存放NULL
composite b*tree index 会存放NULL
bitmap index 会存放NULL
我的理解:
single column b*tree index不存放NULL, 不是出于技术上无法实现 , 而是出于 设计理念和性能考量的目的。
实际上我们可以通过 加入常数字段的方法(create index ind_t1 on test(col,1)) 折中地使用 单字段的索引 存放 NULL 值。
下面的演示 希望说明的是:
1. 在index中存放NULL值 在技术上是可行的
2. 在普通的 b*tree index中NULL值 确实是被当做最大值处理 总是放在索引的右手边的
SQL> create table test (t1 int);
Table created.
SQL> begin
2 for i in 1..99999 loop
3 insert into test values(i);
4 end loop;
5 commit;
6 end;
7 /
SQL> create index ind_t1 on test(t1,1);
Index created.
1 begin
2 for i in 1..99 loop
3 insert into test values(NULL);
4 end loop;
5 commit;
6* end;
PL/SQL procedure successfully completed.
SQL> alter system checkpoint;
System altered.
SQL> exec dbms_stats.gather_table_stats('','TEST',cascade=>TRUE);
PL/SQL procedure successfully completed.
SQL> explain plan for select /*+index_ffs(test ind_t1)*/ t1 from test;
Explained.
SQL> @?/rdbms/admin/utlxplp
PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 3622308455
-------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 100K| 488K| 74 (2)| 00:00:01 |
| 1 | INDEX FAST FULL SCAN| IND_T1 | 100K| 488K| 74 (2)| 00:00:01 |
-------------------------------------------------------------------------------
8 rows selected.
SQL> SELECT object_id FROM dba_objects WHERE object_name ='IND_T1';
OBJECT_ID
----------
81251
SQL> ALTER SESSION SET EVENTS 'immediate trace name treedump level 81251';
Session altered.
SQL> oradebug setmypid;
Statement processed.
SQL> oradebug tracefile_name
/s01/orabase/diag/rdbms/g11r23/G11R23/trace/G11R23_ora_13176.trc
----- begin tree dump
branch: 0x41e131 4317489 (0: nrow: 264, level: 1)
leaf: 0x41e132 4317490 (-1: nrow: 404 rrow: 404)
leaf: 0x41e133 4317491 (0: nrow: 399 rrow: 399)
leaf: 0x41e134 4317492 (1: nrow: 399 rrow: 399)
leaf: 0x41e135 4317493 (2: nrow: 399 rrow: 399)
leaf: 0x41e136 4317494 (3: nrow: 399 rrow: 399)
leaf: 0x41e137 4317495 (4: nrow: 399 rrow: 399)
leaf: 0x41e138 4317496 (5: nrow: 399 rrow: 399)
leaf: 0x41e139 4317497 (6: nrow: 399 rrow: 399)
leaf: 0x41e13a 4317498 (7: nrow: 399 rrow: 399)
leaf: 0x41e13b 4317499 (8: nrow: 399 rrow: 399)
leaf: 0x41e13c 4317500 (9: nrow: 399 rrow: 399)
。。。。。。。。。。。。。。。。。。。。。。。。。
leaf: 0x41e309 4317961 (262: nrow: 153 rrow: 153)
0x41e131 0000000001 0000011110000100110001=> 1 123185 这个是root block
0x41e309 0000000001 0000011110001100001001 1 123657 这个是目前最右手边的leaf block存放了NULL值
SQL> select dump(99999,16) from dual;
DUMP(99999,16)
-----------------------
Typ=2 Len=4: c3,a,64,64
SQL> alter system dump datafile 1 block 123657;
System altered.
row#52[7131] flag: ------, lock: 0, len=17
col 0; len 4; (4): c3 0a 64 63
col 1; len 2; (2): c1 02
col 2; len 6; (6): 00 41 e1 98 01 51
row#53[7114] flag: ------, lock: 0, len=17
col 0; len 4; (4): c3 0a 64 64
col 1; len 2; (2): c1 02
col 2; len 6; (6): 00 41 e1 98 01 52
row#54[7101] flag: ------, lock: 2, len=13
col 0; NULL
col 1; len 2; (2): c1 02
col 2; len 6; (6): 00 41 e1 98 01 53
row#55[7088] flag: ------, lock: 2, len=13
col 0; NULL
col 1; len 2; (2): c1 02
col 2; len 6; (6): 00 41 e1 98 01 54
row#152[5827] flag: ------, lock: 2, len=13
col 0; NULL
col 1; len 2; (2): c1 02
col 2; len 6; (6): 00 41 e1 98 01 b5
SQL> insert into test values(99999);
1 row created.
SQL> insert into test values(1000000);
1 row created.
SQL> insert into test values(100000);
1 row created.
SQL> commit;
Commit complete.
SQL> alter system checkpoint;
System altered.
SQL> select dump(1000000,16) from dual;
DUMP(1000000,16)
-----------------
Typ=2 Len=2: c4,2
SQL> select dump(100000,16) from dual;
DUMP(100000,16)
-----------------
Typ=2 Len=2: c3,b
SQL> alter system dump datafile 1 block 123657;
System altered.
row#52[7131] flag: ------, lock: 0, len=17
col 0; len 4; (4): c3 0a 64 63
col 1; len 2; (2): c1 02
col 2; len 6; (6): 00 41 e1 98 01 51
row#53[7114] flag: ------, lock: 0, len=17
col 0; len 4; (4): c3 0a 64 64
col 1; len 2; (2): c1 02
col 2; len 6; (6): 00 41 e1 98 01 52
row#54[5810] flag: ------, lock: 2, len=17
col 0; len 4; (4): c3 0a 64 64
col 1; len 2; (2): c1 02
col 2; len 6; (6): 00 41 e1 98 01 b6
row#55[5780] flag: ------, lock: 2, len=15
col 0; len 2; (2): c3 0b
col 1; len 2; (2): c1 02
col 2; len 6; (6): 00 41 e1 98 01 b8
row#56[5795] flag: ------, lock: 2, len=15
col 0; len 2; (2): c4 02
col 1; len 2; (2): c1 02
col 2; len 6; (6): 00 41 e1 98 01 b7
row#57[7101] flag: ------, lock: 0, len=13
col 0; NULL
col 1; len 2; (2): c1 02
col 2; len 6; (6): 00 41 e1 98 01 53
row#58[7088] flag: ------, lock: 0, len=13
col 0; NULL
col 1; len 2; (2): c1 02
col 2; len 6; (6): 00 41 e1 98 01 54
row#59[7075] flag: ------, lock: 0, len=13
col 0; NULL
col 1; len 2; (2): c1 02
col 2; len 6; (6): 00 41 e1 98 01 55
row#60[7062] flag: ------, lock: 0, len=13
col 0; NULL
col 1; len 2; (2): c1 02
col 2; len 6; (6): 00 41 e1 98 01 56 |
|