OracleIndex和null研究
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了OracleIndex和null研究,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2083字,纯文字阅读大概需要3分钟。
内容图文
![OracleIndex和null研究](/upload/InfoBanner/zyjiaocheng/546/a82228bca7d84e1b86d265d31ef849f7.jpg)
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
假如有需求:
这时的解决方法: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;
内容总结
以上是互联网集市为您收集整理的OracleIndex和null研究全部内容,希望文章能够帮你解决OracleIndex和null研究所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。