我现在要select 查询获取一个结果集
例如:select a.row1 from table tb a
where a.id=?我现在有很多id ,id =1,2,3,4,5,6,7,......n1,java 里面用for循环
for(int i=1; i<n;i++)
{
调用查询
select a.row1 from table tb a where a.id=i;
}2,直接调用查询
select a.row1 from table tb a where a.id in (1,2,3,4,....n)那个速度快呢?

解决方案 »

  1.   

    2,直接调用查询
    select a.row1 from table tb a where a.id in (1,2,3,4,....n)

    如果in中的列大于1000后,数据库会报错!可以考虑用第2种方式,在java中把in后的控制在1000以内,然后批量提交。
      

  2.   

    2的方法不止快一点。oracle中,能不用循环就尽量不要用。
      

  3.   

    当然是2快了,
    你要明白oracle的工作方式,
    每次提交sql都要有开销,oracle会分配内存,游标,解析语句,验证语法,安全.
    然后生成查询计划,
    最后再开始真正的查询.
      

  4.   

    哥,建一张,序列的临时表吧...........create table t
    (
     ID int
    );--然后运行declare i int;
    begin
    i:=1;
    while i<=10000
    loop
    insert into t values(i);
    i:=i+1;
    end loop;
    end;--10000个ID值不知道够不够--然后关联select a.row1
      from tb as a, (select id from t where id <= n) as b
     where a.row1 = b.id--建议row1有索引.....
      

  5.   

    1、方法性能消耗大
    2、如果ID太多会出现错误的~SQL语句的长度有限制的!
    可以尝试将ID放入数据库中
    然后在数据库做连接查询。我个人感觉这种方式不错!
      

  6.   

    要不在Java程序中用上批处理,或在数据库中创建存储过程再执行,效率会更高。
      

  7.   

    select a.row1 from table tb a where a.id >=1 a.id <= ?;看你循环的就是这个意思撒  从1到n嘛 
      

  8.   

    -- 你一次打过来的许多ID均是一串连续ID串吗?也就是说,有没有“断码”的情况?
    --如果没有的话,还不如直接:
    where a.id>= min_id and a.id<=max_id;
    -- 其中:min_id 是你传过来的的所有ID中最少的ID,max_id是你传过来的所有ID中最大的ID!