select a.fdstrscandate,b.fdstrimei,b.fdstrmodel from tbllocsecond a,  
tbllocfirst b where a.fdstrboxno='DJ2003222' and a.fdstrimei=b.fdstrimei
说明:tbllocfirst表比tbllocsecond大很多,上面的查询非常慢。如果改成以下样子
select a.fdstrscandate,b.fdstrimei from tbllocsecond a,  
tbllocfirst b where a.fdstrboxno='DJ2003222' and a.fdstrimei=b.fdstrimei即去掉了b.fdstrmodel则正常,b.fdstrimei是主键,但b.fdstrmodel也作了索引呀,为什么性能这么差(tbllocfirst表有几百万条记录)。该如何解决呢,请教!谢谢。

解决方案 »

  1.   

    把条件交换一下位置试试
    where a.fdstrimei=b.fdstrimei and a.fdstrboxno='DJ2003222';
      

  2.   

    select a.fdstrscandate,b.fdstrimei from tbllocsecond a,  
    tbllocfirst b where a.fdstrboxno='DJ2003222' and a.fdstrimei=b.fdstrimei

    select fdstrscandate,fdstrimei from tbllocsecond   
    where fdstrboxno='DJ2003222' 
    有什么不同吗?select a.fdstrscandate,b.fdstrimei,b.fdstrmodel from tbllocsecond a,  
    tbllocfirst b where a.fdstrboxno='DJ2003222' and a.fdstrimei=b.fdstrimei
    用到了tbllocfirst表肯定会慢的
      

  3.   

    这样测试一下
    select a.fdstrscandate,b.fdstrimei,b.fdstrmodel from tbllocfirst b,  
    tbllocsecond a where  a.fdstrimei=b.fdstrimei and  a.fdstrboxno='DJ2003222'
      

  4.   

    对这两张表做分析,然后再执行看看oracle是否选用了正确的索引。
    还有就是这两张表分别有哪些索引,主键是怎样定义的。
      

  5.   

    select a.fdstrscandate,b.fdstrimei from tbllocsecond a,  
    tbllocfirst b where a.fdstrboxno='DJ2003222' and a.fdstrimei=b.fdstrimei

    select fdstrscandate,fdstrimei from tbllocsecond   
    where fdstrboxno='DJ2003222' 
    有什么不同吗?
    -----
    可能会不同,我想错了
      

  6.   

    谢谢各位。
    我加了hint语句后,在1秒钟内可以完成/*+ rule */有一点我不太明白,顺便帮忙解释一下:按主键查询时应该是先从主键找到该行,然后从该行中再找其中某列,为什么不同的列有这么大的不同呢?请赐教!
      

  7.   

    采用基于规则的优化方式后1秒内能完成,则说明了oracle在基于成本的优化方式下,因为统计信息的不准确选择了错误的索引或是表扫描。在做了表分析后应该可以不加提示。
    因为查询的列的不同,oracle会可能采用不同的索引,选择那张表做主表,或者决定是否用表扫描。因此查询的速度就会有差异。
    例如,表t有字段ID和NAME两个字段,有一索引idx1是只按NAME建的,在对表做过表分析后,执行
    select name from t where name like '%分公司%'
    oracle会采用索引快速查找(INDEX FAST FULL SCAN)来执行该语句。如果是执行
    select id,name from t where name like '%分公司%'
    则是执行的表扫描。
      

  8.   

    最好帖出优化前后的执行计划对比。
    极有可能是两表关联时的join操作扫表顺序不同造成的。我优化过几个SQL,只是简单地调整循环嵌套连接中内外表的顺序,就将时间由几十秒降到零点几秒。