Access数据库的高效分页

=========================================
以下所有分页方法只能用ID排序分页!不能用其他字段。
//max()分页方法只能根据ID(自增,主键,永远不会重复!),其他字段的话可能会出现重复的值,出现重复的值后用MAX()然后>或<方法就不管用了。
=========================================
access数据库以方便携带,容易部署而受到一些小型系统的喜欢

 

最近在做一个blogs,因为空间的限制,只能用Access数据库

 

测试了一下,当数据量2000+的时候分页比较慢

我的SQL语句是

sql = "select top 10 * from tb_article where id not in (select top " + current * 10 + " id from tb_article order by id desc ) order by id desc";

 

current 是页码

 

随后想到一种:

sql = "select * from tb_article where id between "+10*current +" and "+10*(current+1) +" order by id desc";

不过在删除存在的情况下会有问题

 

 

第三种想到的方法:       

sql = "select top 10 * from tb_article where id >"+current*10 +" order by id desc";

同样,在删除情况下还是会有问题

 

最后又想到一个思路,还是用between

同时新建一个数据表,里面有三个字段,(页码,首记录ID,尾记录ID)

每次分页的时候首先查询这个表

应该速度会加快.

目前仅在思想中,未测试

 

不过此办法比较麻烦,最后使用如下方法

int total = GetListCount(current);


   sql = "select * from tb_article where id between

 

("+total+"-(SELECT MAX(id)   FROM (SELECT TOP " + (current + 1) * 10 + " id FROM tb_article order by id))+1)

and

("+total+"-(SELECT MAX(id)   FROM (SELECT TOP " + current * 10 + " id FROM tb_article order by id)))

 

order by id desc ";

 

posted @ 2011-05-29 23:05  Yaoquan.Luo  阅读(1629)  评论(0编辑  收藏  举报