普通的查询select * from table1 where。。 ,比如我有100个记录,我现在为了提高分页效率,每次只取10个记录,用select* from table1 where 原查询条件 and rownum<10;这样可以只返回前10条记录,得到第一页的数据,那我如何得到第2,第3页的数据啊?(ORACLE中好像没有top关键字)

解决方案 »

  1.   

    select *
    from(
    select *,rownum rn from table1 where 条件)
     where rn>=第N条  and rn<=第n+X条用惯oracle后,你会觉得比ms sql 的top 强大
      

  2.   

    select * 
    from( 
    select *,rownum rn from table1 where 条件) 
     where rn>=第N条  and rn <=第n+X条 
      

  3.   

    我试过了,不能写"*,rownum”,但是可以写成“字段1,字段2... ,rownum”,这样的话写的很麻烦!还有一个问题,2楼的这个例子,oracle的查询优化器是如何处理这个句子的啊,它是不是先查了所有满足where条件的记录,再又到里面去取了x条啊,最终放在内存里面的是所有满足where条件的记录还是只有x个记录啊?
      

  4.   


    你可以这样写
    select aa.* 
    from( 
    select a.*,rownum rn from table1  a where 条件) aa
     where aa.rn>=第N条  and aa.rn <=第n+X条 这条语句先会返回 where 条件里的所有纪录,然后再取where aa.rn>=第N条  and aa.rn <=第n+X条 
    的纪录对于服务器来说,基本上性能没什么优化,和不加分页可能差不多
    但对客户端来说,返回原数据量就少了许多如果你不需要排序的话,可以这样select aa.* 
    from( 
    select a.*,rownum rn from table1  a where 条件 and rownum<=第n+X条 ) aa
     where aa.rn>=第N条  
    这样会好点,服务器会先过滤掉需要的分页数据之后的数据,然后再从里面滤掉分页之前的数据