我这里俩个table,一个hist,一个tbl。
hist中有海量数据,如果查询一周的记录要5分钟,一个月的就要半小时,hist中有个index:histdate
tbl数据较少没有index现在我有俩个查询语句,都是查询一天的数据
select histdate,num,aa from hist
where histdate >= ...
and histdate <=...
用时约0.1mselect histdate,num,bb from tbl
where histdate>=..
and histdate <= ..
用时约0.03m然后我把他们做连接
Select T.*,U.bb from
(select histdate,num,aa from hist
where histdate >= ...
and histdate <=...) T,(select histdate,num,bb from tbl
where histdate>=..
and histdate <= ..) Uwhere T.histdate = U.histdate
and T.num = U.num
连接查询后用时约10m
为什么我做了连接后,效率变得那么慢

解决方案 »

  1.   

    Select T.*,U.bb from
    (select histdate,num,aa from hist
    where histdate >= ...
    and histdate <=...) T,(select histdate,num,bb from tbl
    where histdate>=..
    and histdate <= ..) Uwhere T.histdate = U.histdate
    and T.num = U.num
    你看看2个子查询的数据量多少,数据流大做连接的话,还是比较慢的可以看看执行计划,看是否是连接上造成的时间消耗
      

  2.   

    T结果有17行
    U18行执行计划怎么看呢,我用的PL/SQL工具
      

  3.   


    Select T.*,U.bb from
    (select histdate,num,aa from hist
    where histdate >= ...
    and histdate <=...) T,(select histdate,num,bb from tbl
    where histdate>=..
    and histdate <= ..) U
    /*
    where T.histdate = U.histdate
    and T.num = U.num*/
    我把连接条件注掉,300条结果,用时0.15m
    为什么呢
      

  4.   

    你的写法有问题。select * from table1 t1,table2 t2 where to_char(t1.sj,'yyyy-mm-dd') between '' and '' and t1.sj=t2.sj 
     
      

  5.   


    Select T.histdate,T.num,T.aa,U.bb from
    hist T,
    tbl U
    where 
    T.histdate >= ... and T.histdate <=...
    and U.histdate >= ... and U.histdate <=...
    and T.histdate = U.histdate
    and T.num = U.num
      

  6.   

    为什么要分开写呢,写成一个简单的连接SQL,给第二张表建上索引,基本上不会有什么问题。
      

  7.   


    看执行计划,或者更进一步做个autotrace或sqltrace,用数据说话