操作系统:win2003
数据库:oracle9i
数据量:lk这张表有500万条左右,ba_code有100条左右(记录)
select * from
(
select l.*,b1.name 
from lk l,ba_code b1
where l.strt = b1.code(+)
and l.date >= to_date('2009-05-23','yyyy-mm-dd')
and l.date <= to_date('2009-05-25','yyyy-mm-dd')
and l.ename like 'LIN%'
order by l.lk_id
)
where rownum <=100我的lk_id为主键
不加order by l.lk_id这句,执行时间不到一分钟
可加上这句执行时间超过十分钟
感觉数据库索引没利用上,还是其他什么原因,请指教,谢谢!

解决方案 »

  1.   

    select * from (
    select * from (
    select * from lk l where l.date >= to_date('2009-05-23','yyyy-mm-dd') 
    and l.date <= to_date('2009-05-25','yyyy-mm-dd') 
    and l.ename like 'LIN%' order by l.lk_id
    ) where rownum<=100
    ) a,ba_code b 
    where a.strt = b.code(+) 
      

  2.   


     在内嵌sql中并没有使用到索引lk_id,sql中的执行顺序 先where 再select 后order, 
    以下的语句组成了一个新表,order 的时候 不是简单的通过主键l.lk_id进行排序 
    select l.*,b1.name 
    from lk l,ba_code b1 
    where l.strt = b1.code(+) 
    and l.date >= to_date('2009-05-23','yyyy-mm-dd') 
    and l.date <= to_date('2009-05-25','yyyy-mm-dd') 
    and l.ename like 'LIN%' 
      

  3.   

    看看你的语句执行顺序,
    select l.*,b1.name 
    from lk l,ba_code b1 
    where l.strt = b1.code(+) 
    and l.date >= to_date('2009-05-23','yyyy-mm-dd') 
    and l.date <= to_date('2009-05-25','yyyy-mm-dd') 
    and l.ename like 'LIN%' 
    这段查询出来的结果就不会很小,再排序,这样速度可定不会快的
    select l.*,b1.name 
    from lk l,ba_code b1 
    where l.strt = b1.code(+) 这段出来的数据有500w,再查满足条件的,这样不会快吧先查lk表中满足日期和ename条件的数据再进行连接这样速度会快的吧
      

  4.   


    1、按我的方法关联再查询 用了835秒
    2、先查lk表的符合条件的数据,然后再关联 用了819秒

    select * from ( 
    select * from ( 
    select * from lk l where l.date >= to_date('2009-05-23','yyyy-mm-dd') 
    and l.date <= to_date('2009-05-25','yyyy-mm-dd') 
    and l.ename like 'LIN%' order by l.lk_id 
    ) where rownum <=100 
    ) a,ba_code b 
    where a.strt = b.code(+)
    速度差不多,这样看来关键再于那个order by l.lk_id比较慢
    有什么其他比较好的解决方法吗?