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
Oracle查询指定索引提高查询效率
最新推荐文章于 2023-03-21 08:30:30 发布