select a.col1,a.col2 from table a where (1=1) 
and exists (select b.col1,b.col2 from table2 b where a.id=bid)
and b.name like '上海科技%'
其中table1和table2 中的数据量都是比较大的 我的table2表中建立的name的索引 但是我分析sql后只用了id 这个索引却没有使用name上的索引 ,查询很慢,请问这是怎么一回事

解决方案 »

  1.   

    试试看with tmp as
    (
     select * from table2 where name like '上海科技%'
    )
    select a.col1,a.col2 
      from table a 
     where exists (select 1 
                     from tmp b 
                    where b.id = a.id
                   );
      

  2.   

    试试id ,name联合索引   
      

  3.   

    多个条件列有多个单独索引,一般只会用到一个索引。
    至于使用那一个索引,要看优化模式。
    基于COST优化是目前最好的优化模式,你只要搜集好统计数据,执行计划交给ORACLE去选择,
    在这种情况下,你自已选择的执行计划一般来说不是最好的。
    另外,还要说明的是并不是条件字段中的每一个索引用到就是好的!!
    如果你要同时使用一个表中多个索引,可以使用and_equal提示。
      

  4.   

    like是不走索引的 是要全表扫描的
      

  5.   

    在b表上建立(id, name)联合索引试试
      

  6.   

    一个表在一次查询中只能使用一个索引。你这种情况考虑建一个包含id和name字段的组合索引
      

  7.   

    like是不走索引的,建了索引页没用
      

  8.   

    select a.col1,a.col2 from table a where (1=1)  
    and exists (select b.col1,b.col2 from table2 b where a.id=bid)
    and b.name like '上海科技%'
    这样的代码可以跑?
      

  9.   

    like 在左匹配的 好像是走 索引的
      

  10.   

    1、有没有进行表的统计
    2、可以尝试使用HINT强制指定索引
      

  11.   

    如果索引是建立在多个列上, 只有在它的第一个列被where子句引用时,优化器才会选择使用该索引。
    例如:
    table02 的复合索引:a001、aa140、aaa041
    Select * from table02 where aa140=’31’ and aaa041=’200801’; --不会使用索引
    Select * from table02 where a001=’10001000’; --可以使用索引
    如果不使用索引第一列基本上不会使用索引,使用索引要按照索引的顺序使用,另外使用复合索引的列越多,查询的速度就越快
      

  12.   

    加了注释快多了?
    我想你应该说的是hint吧 这个写法还真像是注释
      

  13.   

    像楼上说的一样,你说的应该是hint, 强制oracle按照你的规则去使用索引。
      

  14.   

    楼主语法比较怪,这样试试呢
    elect a.col1,a.col2 from table a where (1=1)  
    and exists (select b.col1,b.col2 from table2 b where a.id=bid and b.name like '上海科技%'
    )
      

  15.   

    select a.col1,a.col2 from table a where (1=1)   
    and exists (select 1 from table2 b where a.id=bid and b.name like '上海科技%'
    )
      

  16.   

    你可以用hint或者函数索引instr()来替换 like
      

  17.   


    LZ的SQL写错了吧。 看执行计划,索引要建就要建选择性好的。
      

  18.   

    把like用instr换掉,建立函数索引,
    就是不晓得可以在id,instr(name) 这两个上建立联合索引不?如果可以,那就好了。。呵呵呵