1. 程式人生 > 其它 >Oracle DML操作時索引不會維護空值(NULL)

Oracle DML操作時索引不會維護空值(NULL)

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
; /
View Code

實際測試實驗如下:

有無索引
插入值 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