比如有一个表叫做table1,包含200万条记录。
其中字段a,有大约2000种可能。其他字段包括b,c,d,e,f就这么多吧。
a,b,c,d,e,f都可以作为可供排序的字段,其中a上面建有索引。
现在用户指定a字段的一个子集,可能只有一项也可能是2000种。
用户还给定了排序字段的顺序和升降关系。要求查询满足用户要求
的结果集(可以只返回前若干(比如10000)条记录)。
我曾经使用的方法有:
1.select * from (select * from table1 order by b,c desc,d...) where a=x1 or a=x2 or ... and rownum<=10000很长;
2.select * from (select * from table1 order by b,c desc,d...) where a in (x1,x2...) or a in (x100,x101...) and rownum<=10000;
  因为好像in后面的数据项不能太多。
3.先把所有需要的a的值insert到一个表table2里,之后select * from (select * from table1 order by b,c desc,d...) t1, table2 t2
  where t1.a=t2.a and rownum<=10000;
  
但是这三种方式都不能满足我的要求,如果where子句不长,或者不进行排序的话,还是可以较快返回的。

解决方案 »

  1.   

    第三种方法的问题主要在什么地方
    select * from table1 where a in 
    (select a from table2)
    and rownum<=10000不过table1一定要在a字段建索引
      

  2.   

    murphyxiao(头文字) 所提的方法:
    select * from table1 where a in 
    (select a from table2)
    and rownum<=10000
    和我所写的第三种方法性能差不多,
    时间上面也是最快的一种,实在不行就只能用它了。至于为什么先排序,是因为我要通过限制结果集大小提高时间,只能先排好顺序。
    而且好像排序不是性能的瓶颈!