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分钟,应用于网页查询,简直难以忍受。
如下优化:
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