就是我发帖的那句 例如: select * from bbb where sid in(1,2,3,4); . . . . . . . . select * from bbb where sid in(11,12,13,14,2,32);同时查询的人比较多,这个时候效率非常慢,还会导致jsp页面卡死。
原始: select * from aaa where id in (1,2,3,4);分析: 1:表aaa的id字段有无索引 2:aaa的数据量大概多少 3:括号中的1,2,3,4是常量还是要通过关联别的表来查询,如果是子查询,查询这些值的效率如何 4:用in、exists、or只能等你每一种都试一下之后做了对比才知道 5:语法问题可以谷歌。
使用动态sql试试呢 str:='select * from bbb where sid =:1'; execute immediate str using 1; execute immediate str using 2;
这样吧,其实in的执行不是大家所想象的那样不走索引。因为Oracle优化器是会对这个语句进行优化。比如该语句select * from aaa where id in (1,2,3,4);会变成4条语句:select * from aaa where id= '1';select * from aaa where id= '2',select * from aaa where id= '3';select * from aaa where id= '4',然后合并结果集。如果大家想深入研究,可以看看相关方面的书籍。很多书会对此有描述。
select * from bbb where sid in(1,2,3,4); 首选把*换成你要查询的所有列(用*会降低查询速度),然后把in去掉使用简单的条件语句,查询效率应该会提升30%.
id=1 ||id=2 ||id=3 ||id=4
这个思路OK,因为in不走索引,而用‘or’分隔 可以提高效率。
如果可以,尝试exists(not exists)代替in(not in)
用 exists怎么做 请给个例子,因为要查的包含多个数据 in(1,2,3,4,5);
就是我发帖的那句
例如:
select * from bbb where sid in(1,2,3,4);
.
.
.
.
.
.
.
.
select * from bbb where sid in(11,12,13,14,2,32);同时查询的人比较多,这个时候效率非常慢,还会导致jsp页面卡死。
select * from aaa where id in (1,2,3,4);分析:
1:表aaa的id字段有无索引
2:aaa的数据量大概多少
3:括号中的1,2,3,4是常量还是要通过关联别的表来查询,如果是子查询,查询这些值的效率如何
4:用in、exists、or只能等你每一种都试一下之后做了对比才知道
5:语法问题可以谷歌。
execute immediate str using 1;
execute immediate str using 2;
1:有索引
2:一万以内
3:in里的数字每次都不一样,是从前台提取10~50个数传到in里
同时往in里传值的用户大概在2000以内
怎么能提高效率
or与in没有什么区别,exists做不了这个情况 只能单独查询某个数
select * from test t where not exists (select NULLIF(0,select decode(ts.id,1,0,2,0,3,0,t.id) from test ts where ts.id=t.id)) 去试下 我没试
1,2,3,4 是我举的例子,每次查询传进来的都不是一样的,而且exists是查询子句的。你说那怎么替代in
楼主仅给出 sql范例,求优化,很难。其实,90%是和业务结合在一起的。看原始的业务需求,然后看设计的思路,最后才是优化语句。
首选把*换成你要查询的所有列(用*会降低查询速度),然后把in去掉使用简单的条件语句,查询效率应该会提升30%.
union all
select * from bbb where sid =2
union all
select * from bbb where sid =3
union all
....
如何?