每日一得

不求多得,只求一得 about java,hibernate,spring,design,database,Ror,ruby,快速开发
最近关心的内容:SSH,seam,flex,敏捷,TDD
本站的官方站点是:颠覆软件

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  220 随笔 :: 9 文章 :: 421 评论 :: 0 Trackbacks
key words :Oracle分页 视图

google了一下关于Oracle的分页方法,方法还不少,大多数效果差不多-有点恶心. 恶心也要作,不过后来就是大家都用得这种方式在我这里出现了新问题,奇怪的是怎么没有别人碰到?

String condition = " teacher_id = " + userId + " and school_id="+siteId;
sql 
=
   
" select * " +
   
" from your_table where " + condition +
   
" and rowid not in ( select rowid from your_table where" + condition +
   
" and rownum <= " + (pageIndex - 1* Constants.PAGE_NUMS + "" +
   
" and rownum <= " + Constants.PAGE_NUMS ;

现在的问题是我需要按照table的某个字段排序,于是改成如下:
String condition = " teacher_id = " + userId + " and school_id="+siteId;
sql 
=
    
" select * " +
    
" from your_table where " + condition +
    
" and rowid not in ( select rowid from your_table where" + condition +
    
" and rownum <= " + (pageIndex - 1* Constants.PAGE_NUMS + "order by id desc" +
    
" and rownum <= " + Constants.PAGE_NUMS + " order by id desc";

这个sql有问题么?
答案是可能有问题,也可能没有问题,因为据说在8i的Oracle版本之前都不行,实际上也不尽然,在我的9i和10g我得到的是同样的错误 "missing right parenthesis",还有一位兄弟估计是DBA建议我去metalink打一个patch,埃,动作太大了,不敢动。

问题还是要解决,试了下类似于select a.*,rownum r from (select * from table where ...) a where rownum < 10 等的方法,效果一样,就是不能加嵌套的order by
最后,用视图的方法间接解决问题,因为我要解决的问题实际就是按某个字段排序,那么在视图里先对table进行排序,再在视图的基础上作操作就OK了.

另,还有一种不错的实现方法,即用OracleCachedRowSet,分页也比较简单,有点类似于hibernate,由于时间关系没有时间去看,感兴趣的朋友可以看一下.


BTW: 对于视图可能rowid有问题,可以改成视图的某个主键替换

String condition = " teacher_id = " + userId + " and school_id="+siteId;
sql 
=
    
" select * " +
    
" from your_table where " + condition +
    
" and id not in ( select id from your_table where" + condition +
    
" and rownum <= " + (pageIndex - 1* Constants.PAGE_NUMS + "order by id desc) " +
    
" and rownum <= " + Constants.PAGE_NUMS + " order by id desc";



posted on 2006-09-20 15:36 Alex 阅读(1677) 评论(3)  编辑  收藏 所属分类: dataBase

评论

# re: Oracle分页的一个问题 2006-09-21 11:03 效率
效率,注意效率.
如果数据在百万条以上.嵌套将是很痛苦的.  回复  更多评论
  

# re: Oracle分页的一个问题 2006-09-21 15:08 Alex
效率确实是个问题,问题是在目前所看到的Oracle分页方案中几乎都用到了嵌套,有什么别的办法么?  回复  更多评论
  

# re: Oracle分页的一个问题 2006-09-21 17:32 123bingbing
看新闻这么麻烦,找资料还是去www.mylinux.com.cn
www.mylinux.com.cn网上软件外包流水线,找资料,看新闻,接项目,积分换数码产品  回复  更多评论
  


只有注册用户登录后才能发表评论。


网站导航: