Oracle DML操作时索引不会维护空值(NULL)
如果一个字段有索引,以insert为例,插入时候字段的值为NULL,不会维护该字段的索引。

create table zkm.test (id int); create index zkm.idx_id on zkm.test(id); begin for i in 1..1000000 loop insert into zkm.test values (null); end loop; end; / begin for i in 1..1000000 loop insert into zkm.test values (1); end loop; end; /
实际测试实验如下:
有无索引 | 有 | 无 | ||
插入值 | NULL | 1 | NULL | 1 |
执行时间(s) | 22.72 | 34.73 | 22.40 | 21.59 |
1.无索引,插入NULL值。
15:42:06 SYS@testdb(330)> create table zkm.test (id int); Table created. Elapsed: 00:00:00.01 15:42:09 SYS@testdb(330)> begin 15:42:14 2 for i in 1..1000000 loop 15:42:14 3 insert into zkm.test values (null); 15:42:14 4 end loop; 15:42:14 5 end; 15:42:14 6 / PL/SQL procedure successfully completed. Elapsed: 00:00:22.40
2.有索引,插入NULL值
15:42:37 SYS@testdb(330)> drop table zkm.test purge; Table dropped. Elapsed: 00:00:00.15 15:43:25 SYS@testdb(330)> create table zkm.test (id int); Table created. Elapsed: 00:00:00.01 15:43:31 SYS@testdb(330)> create index zkm.idx_id on zkm.test(id); Index created. Elapsed: 00:00:00.00 15:43:33 SYS@testdb(330)> begin 15:43:37 2 for i in 1..1000000 loop 15:43:37 3 insert into zkm.test values (null); 15:43:37 4 end loop; 15:43:37 5 end; 15:43:37 6 / PL/SQL procedure successfully completed. Elapsed: 00:00:22.72
3.无索引,插入非NULL值
15:44:50 SYS@testdb(330)> drop table zkm.test purge; Table dropped. Elapsed: 00:00:00.14 15:44:51 SYS@testdb(330)> create table zkm.test (id int); Table created. Elapsed: 00:00:00.00 15:44:53 SYS@testdb(330)> begin 15:44:56 2 for i in 1..1000000 loop 15:44:56 3 insert into zkm.test values (1); 15:44:56 4 end loop; 15:44:56 5 end; 15:44:56 6 / PL/SQL procedure successfully completed. Elapsed: 00:00:21.59
4.有索引,插入非NULL值
15:45:49 SYS@testdb(330)> drop table zkm.test purge; Table dropped. Elapsed: 00:00:00.15 15:45:55 SYS@testdb(330)> create table zkm.test (id int); Table created. Elapsed: 00:00:00.01 15:45:57 SYS@testdb(330)> create index zkm.idx_id on zkm.test(id); Index created. Elapsed: 00:00:00.01 15:45:59 SYS@testdb(330)> begin 15:46:02 2 for i in 1..1000000 loop 15:46:02 3 insert into zkm.test values (1); 15:46:02 4 end loop; 15:46:02 5 end; 15:46:02 6 / PL/SQL procedure successfully completed. Elapsed: 00:00:34.73