基于函数的索引

有时可能要在 where 字句中使用表达式.如果在where 字句的表达式或者函数里面已经包含某个列,
则不会使用该列上面的索引. 为了方便此类操作oracle 提供了一个选项,可以基于一个或者多个列上的函数
或者表达式上面创建索引.

select * from app_accountpro t where t.acc_name='' --0.125秒
select * from app_accountpro t where upper(t.acc_name)=''---- 29.4秒
在此函数上建立索引
create index index_accname on app_accountpro(upper(acc_name));
再次查询
select * from app_accountpro t where upper(t.acc_name)=''---- 0.141秒

在自定义函数上建立索引
create or replace function CONVERT_ID_15 (/*转换身份证号为15位*/
  p_id2 in varchar2
) return varchar2
is
  p_id varchar2(20);
  id varchar2(15);
begin
  p_id:=ltrim(p_id2);
  p_id:=rtrim(p_id);

  if length(p_id) = 18 then
    id:=substr(p_id, 1, 6) || substr(p_id, 9, 9);
  else
    id:=substr(p_id, 1, 15);
  end if;

  return id;
exception
  when others then
    return null;
end
;

create index index_hf_id on app_accountpro(CONVERT_ID_15(hf_id))
报错ORA-30553: 函数不能确定(ORA-30553: The function is not deterministic)
修改视图
create or replace function CONVERT_ID_15 (/*转换身份证号为15位*/
  p_id2 in varchar2
) return varchar2 deterministic
is
  p_id varchar2(20);
  id varchar2(15);
begin
  p_id:=ltrim(p_id2);
  p_id:=rtrim(p_id);

  if length(p_id) = 18 then
    id:=substr(p_id, 1, 6) || substr(p_id, 9, 9);
  else
    id:=substr(p_id, 1, 15);
  end if;

  return id;
exception
  when others then
    return null;
end
;
创建索引成功.
未创建索引之前
select t.hf_id from app_accountpro t where CONVERT_ID_15(t.hf_id)='430221870924382';--59.412秒
创建索引之后 0.078秒.

另外索引还可以基于表达式
create index on app_tab1(num1*num2);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值