Oracle查询指定索引提高查询效率

oracle查询指定索引提高查询效率
一个1600万数据表--短信上行表tbl_sms_mo
结构:
create table tbl_sms_mo
(
 sms_id number,
 mo_id varchar2(50),
 mobile varchar2(11),
 spnumber varchar2(20),
 message varchar2(150),
 trade_code varchar2(20),
 link_id varchar2(50),
 gateway_id number,
 gateway_port number,
 mo_time date default sysdate
);
create index idx_mo_date on tbl_sms_mo (mo_time)
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 1m
    next 1m
    minextents 1
    maxextents unlimited
    pctincrease 0
  );
create index idx_mo_mobile on tbl_sms_mo (mobile)
  pctfree 10
  initrans 2
  maxtrans 255
  storage
  (
    initial 64k
    next 1m
    minextents 1
    maxextents unlimited
    pctincrease 0
  );

问题:从表中查询某时间段内某手机发送的短消息,如下sql语句:

select mobile,message,trade_code,mo_time
from tbl_sms_mo
where mobile=’130xxxxxxxx’
and mo_time between to_date(’2006-04-01’,’yyyy-mm-dd hh24:mi:ss’) and to_date(’2006-04-07’,’yyyy-mm-dd hh24:mi:ss’)
order by mo_time desc
返回结果大约需要10分钟,应用于网页查询,简直难以忍受。
 
分 析:
在pl/sql developer,点击“explain plan”按钮(或f5键),对sql进行分析,发现缺省使用的索引是 idx_mo_date。问题可能出在这里,因为相对于总数量1600万数据来说,都mobile的数据是很少的,如果使用idx_mo_mobile比 较容易锁定数据。
 
如下优化:
select /*+ index(tbl_sms_mo idx_mo_mobile) */ mobile,message,trade_code,mo_time
from tbl_sms_mo
where mobile=’130xxxxxxxx’
and mo_time between to_date(’2006-04-01’,’yyyy-mm-dd hh24:mi:ss’) and to_date(’2006-04-07’,’yyyy-mm-dd hh24:mi:ss’)
order by mo_time desc

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值