这是肯定的,在加了索引的字段上操作函数肯定会很慢的,不说instr,连trim这些函数都不行。 我提供一点建议哈:用like select * from emp e where a.lot_id like '%'||b.lot_id||'%'
晕,刚才 sql让人看不懂,看where条件后就行了。 a.lot_id like '%'||b.lot_id||'%'
create index idx_lot_id on rpt_lotinfo a inner join (select a.lot_id from rpt_lotinfo a,rpt_wipstep b where a.lot_id = b.lotnum group by a.lot_id) b (instr(a.lot_id,b.lot_id));请问我这个索引这样写可不可以?
估计楼主加的索引类型不对啊!可以尝试: 1、函数索引。 2、我给个列子(好像10g中才有)。 create index mytext_id_x on mytext(thetext) indextype is CTXSYS.CONTEXT; select * from mytext where contains(thetext,'my')>0;
字符串函数操作是不算是最快的,但楼主的问题不是因为函数的引起的,也和索引基本无关。表a和b除了instr(a.lot_id,b.lot_id,1,1) > 0这个条件外,还有其它的关联字段吗?关联的程度如何?也就是a中的一条记录关联在b中的几条记录? 如果只有这个instr条件,那执行的代价就巨大无比了。假定a,b表中各有1万条记录,那oracle就必须执行1万x1万=1亿次instr运算。 如果a,b表还有其它的相关列,比如 SELECT COUNT(1) FROM a, b WHERE a.id = b.id AND instr(a.lot_id,b.lot_id,1,1) > 0; 假定关联程度为2,那通过a.id = b.id条件,Oracle就能把执行范围降低到2万个记录,执行2万次和1亿次instr运算的差异当然是不可比的。
我提供一点建议哈:用like
select * from emp e where a.lot_id
like '%'||b.lot_id||'%'
a.lot_id like '%'||b.lot_id||'%'
(select a.lot_id
from rpt_lotinfo a,rpt_wipstep b
where a.lot_id = b.lotnum
group by a.lot_id) b (instr(a.lot_id,b.lot_id));请问我这个索引这样写可不可以?
1、函数索引。
2、我给个列子(好像10g中才有)。
create index mytext_id_x on mytext(thetext) indextype is CTXSYS.CONTEXT;
select * from mytext where contains(thetext,'my')>0;
向这样使用,即使在a.lot_id,b.lot_id各个字段上加了索引,oracle也不会走索引的。因为你在该字段上使用了函数,所以其不走索引。除非向楼上说的那样建立函数索引。
如果只有这个instr条件,那执行的代价就巨大无比了。假定a,b表中各有1万条记录,那oracle就必须执行1万x1万=1亿次instr运算。
如果a,b表还有其它的相关列,比如
SELECT COUNT(1)
FROM a, b
WHERE a.id = b.id
AND instr(a.lot_id,b.lot_id,1,1) > 0;
假定关联程度为2,那通过a.id = b.id条件,Oracle就能把执行范围降低到2万个记录,执行2万次和1亿次instr运算的差异当然是不可比的。