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上的索引 ,查询很慢,请问这是怎么一回事
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上的索引 ,查询很慢,请问这是怎么一回事
(
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
);
至于使用那一个索引,要看优化模式。
基于COST优化是目前最好的优化模式,你只要搜集好统计数据,执行计划交给ORACLE去选择,
在这种情况下,你自已选择的执行计划一般来说不是最好的。
另外,还要说明的是并不是条件字段中的每一个索引用到就是好的!!
如果你要同时使用一个表中多个索引,可以使用and_equal提示。
and exists (select b.col1,b.col2 from table2 b where a.id=bid)
and b.name like '上海科技%'
这样的代码可以跑?
2、可以尝试使用HINT强制指定索引
例如:
table02 的复合索引:a001、aa140、aaa041
Select * from table02 where aa140=’31’ and aaa041=’200801’; --不会使用索引
Select * from table02 where a001=’10001000’; --可以使用索引
如果不使用索引第一列基本上不会使用索引,使用索引要按照索引的顺序使用,另外使用复合索引的列越多,查询的速度就越快
我想你应该说的是hint吧 这个写法还真像是注释
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 '上海科技%'
)
and exists (select 1 from table2 b where a.id=bid and b.name like '上海科技%'
)
LZ的SQL写错了吧。 看执行计划,索引要建就要建选择性好的。
就是不晓得可以在id,instr(name) 这两个上建立联合索引不?如果可以,那就好了。。呵呵呵