Oracle Index 和null 研究

WBOY
풀어 주다: 2016-06-07 17:35:47
원래의
899명이 탐색했습니다.

Oracle Index 和null 研究,这里1可以是任意数字或字母。而这时,select语句的谓词不需要更改,还是t_name is null.

Indexing null values

安装关系数据库理论, null表示未知,Oracle b-tree index是不包含null的。考虑如下表:

create table tt (t_id number, t_name varchar2(10));
create index tt_idx on tt(t_name);

select * from tt where t_name is null是不会使用index scan的,这经常会造成性能问题。

解决办法就是:创建一个函数索引,并在select 中包含该函数,如:

create index tt_idex on tt( nvl(t_name), 1);
select * from tt where nvl(t_name,1)=1;

从11g开始有另一个方法:

create index tt_idx on tt(t_name asc, 1);

这里1可以是任意数字或字母。而这时,select语句的谓词不需要更改,还是t_name is null.

Uniqueness and null

drop index tt_idx;
create unique index tt_idx on tt(t_name);
insert into tt values(1, null);
commit;
insert into tt values(1, null);
commit;

这段SQL可以执行成功。这是因为null不被索引包含。

create table ttt2  (tt2_id number, tt21 varchar2(10), tt22 varchar2(10));
create unique index ttt2_idx on ttt2(tt21, tt22);
--Successful
insert into ttt2 values(1, null, null);
insert into ttt2 values(1, null, null);
commit;
--Fail
insert into ttt2 values(1, '1', null);
insert into ttt2 values(1, '1', null);
commit;

第二个事务会失败。因为null不被索引包含, 两个'1'就是重复值!

 

Conditional uniqueness

假如有需求:

  • tt21, tt22都可以为null
  • 仅在tt21和tt22都不为null时,,需要保证唯一性!
  • 这时的解决方法:Function based index 。

    create table ttt2  (tt2_id number, tt21 varchar2(10), tt22 varchar2(10));
    create or replace function conditional_uniqueness(p_tt21 varchar2, p_tt22 varchar2)
    return varchar2
    DETERMINISTIC
    as
    begin
      if(p_tt21 is not null and  p_tt22 is not null) then
        return p_tt21||p_tt22;
      else
        return null;
      end if;
    end;
    create unique index ttt2_idx on ttt2(conditional_uniqueness(tt21, tt22));
    --Fail!
    insert into ttt2 values(1, '1','1');
    insert into ttt2 values(1, '1','1');
    --Successful
    insert into ttt2 values(1, '1',null);
    insert into ttt2 values(1, '1',null);
    --Successful
    insert into ttt2 values(1, null,'1');
    insert into ttt2 values(1, null,'1');
    commit;

    linux

    원천:php.cn
    본 웹사이트의 성명
    본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
    최신 이슈
    인기 튜토리얼
    더>
    최신 다운로드
    더>
    웹 효과
    웹사이트 소스 코드
    웹사이트 자료
    프론트엔드 템플릿
    회사 소개 부인 성명 Sitemap
    PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!