日期:2014-05-18  浏览次数:20724 次

大型数据库检索结果分页显示问题
public   class   markPage
{
private   int   num_per   =   4;
private   int   num_rs   =   0;
private   int   pages_all   =   0;
private   int   page_current   =   1;
ResultSet   rs   =   null;
DBOperation   db   =   null;

public   void   setPageInfo(int   page,String   sql)
{
//设置当前页码
try
{
db   =   new   DBOperation();
rs   =   db.excuteQuery(sql);//db在四个大型数据库中执行完整的sql语句,为什么这句执行的时候特别慢
if(rs.last())
this.num_rs   =   rs.getRow();
this.pages_all   =   (this.num_rs   %   this.num_per   ==   0)?(num_rs/num_per):(num_rs/num_per)+1;
if(page   <   1)
this.page_current   =   1;
else   if(page   >   this.pages_all)
this.page_current   =   this.pages_all;
else
this.page_current   =   page;
}
catch(Exception   e)
{
e.printStackTrace();
}
}
public   ResultSet   getPageRs()
{
//返回当前页的数据集
try
{
int   pos   =   (this.page_current   -   1)*num_per   +   1;
rs.absolute(pos);
}
catch(Exception   e)
{
e.printStackTrace();
}
return   rs;
}
public   void   closed()
{
this.db.closeConn();
}
public   int   getNumber()
{
return   this.num_per;
}
public   int   getNumbers()
{
return   this.num_rs;
}
public   int   getPagesAll()
{
return   this.pages_all;
}
public   int   getPageCurrent()
{
return   this.page_current;
}
}

------解决方案--------------------
如果是数据量很大的数据表的话,你这种根本就不是分页。你这种是从数据表中取出所有的,再截取一小部分,而把那些花较长时间从数据库中取出的数据就扔了。

为了提高数据库的连接效率,你还需要设置数据库连接池,效率会以几何级的速度增加。

正确的分页是使用分页的 SQL 语句,也就是说,要第几页的数据(假设每页显示 10 条),就只从数据库中取出这一页的 10 条数据,不取没用的。

分页的 SQL 语句,各种数据库的写法不一样,可以去搜索一下。
------解决方案--------------------
对于大表,就不要用这种方法来分页,可以用这个试试: select top 10 * from a where id not in(select top 10 id from a order by id desc) order by id desc

这条语句能把第2页的数据取出,仅供参考!