大数据量下的分页

 

大数据量下的分页

    对于非常大的数据模型而言,分页检索时,每次都加载整个数据源非常浪费。通常的选择是检索页面大小的块区的数据,而非检索所有的数据,然后单步执行当前行。

    本文演示ASP.net的DataGrid和Sql Server 实现大数据量下的分页,为了便于实现演示,数据表采用了Northwind数据库的Orders表(830条记录)。

    如果数据表中有唯一的自增索引,并且这个字段没有出现断号现象。检索页面大小的块区数据就非常简单了。通过简单的Sql语句就可以实现这个功能:

    
select * from orders where orderid between 10248 and 10253

    其中,开始编号为:(CurrentPageIndex - 1) * PageSize  结束编号为:CurrentPageIndex * PageSize

    当然,如果这个字段断号不是很严重,而且允许不是很严格的按照每页条数分页,这样的方法也是可以用的。

    如果这个字段断号,或者需要按照其他条件排序分页,就要复杂些了。首先要获得这个页面需要显示的编号,然后再按照这个编号获得需要的块区数据。根据编号获得块区数据很简单。不过用下面方式获得数据排序并不是按照指定的id列表顺序,这时候还要附加order by 命令。

select * from orders where orderid in (10248,10249,10250,10251,10252,10253) order by orderid desc

    获得这个页面需要显示的编号列表就复杂多了,而且有多种方案:

方案一:维护一个表,这个表记录需要显示的这些编号排序顺序。(这个表可以是临时表,也可以是物理表)。下面演示了利用一个全局临时表。这个全局临时表记录需要显示的编号。注意排序,这里的order by 就是需要显示的排序顺序。

create table ##temptable(
iid int IDENTITY (1, 1) NOT NULL,
mainid int NOT NULL
)

insert ##temptable(mainid) select OrderID from orders order by OrderID desc

select * from ##temptable

drop table ##temptable -- 实际执行时候,删除全部临时表当然不再这里执行。

这个临时表存在,获得指定分页的分块数据就很简单了。看下面代码:

create table ##temptable(iid int IDENTITY (1, 1) NOT NULL,mainid int NOT NULL)
insert ##temptable(mainid) select OrderID from orders order by OrderID desc
declare @PageSize int,@CurrPage int,@strSQL varchar(2000),@IDStr varchar(1000)
select @PageSize = 30
select @CurrPage = 2
select @IDStr = ''
select @IDStr = @IDStr + ltrim(rtrim(str(MainID))) + ',' from ##temptable 
where iid between ((@CurrPage-1)*@PageSize+1) and @CurrPage*@PageSize
if @IDStr <> '' 
begin
 select @IDStr = left(@IDStr,len(@IDStr)-1)
end
select @strSQL = 'select * from orders where OrderID in ('+@IDStr+')  order by OrderID desc '
exec(@strSQL)
drop table ##temptable

注意:实际使用这个方案的时候,还要考虑何时更新这个全局临时表,一般是放到计划任务中,定时更新这个汇总表。

方案二:每次都去查询,每次获得最新的编号顺序。由于这时候不存在这个临时表,书写获得需要显示页面的编号的字符串就需要点技巧,看下面的代码:

declare @PageSize int,@CurrPage int,
@topnum int,@previous int
select @PageSize = 30
select @CurrPage = 2
select @topnum = @CurrPage * @PageSize
select @previous = (@CurrPage - 1) * @PageSize
declare @i int,@IDStr nvarchar(500),@strSQL nvarchar(1000)
select @i = 0
select @strSQL = N''
select @strSQL = @strSQL + N' select top '+str(@topnum)+ ' @i = @i + 1 '
select @strSQL = @strSQL + N',  @IdStr = '
select @strSQL = @strSQL + N'case when @i > '+str(@previous)+' then  @IdStr + ltrim(rtrim(str(OrderID))) + '','' '
select @strSQL = @strSQL + N'else N''''end '
select @strSQL = @strSQL + N'from Orders '
select @strSQL = ltrim(rtrim(@strSQL)) + N' order by OrderID desc '
Select @IdStr = N''
exec sp_executesql @strSQL,N'@i int,@IdStr varchar(500) output',@i,@IdStr output
if len(rtrim(ltrim(@IdStr))) > 0
begin
 select @IdStr = left(@IdStr,len(@IdStr)-1)
end
select @strSQL = 'select * from orders where OrderID in ('+@IDStr+')'
exec(@strSQL)

ASP.net 的 DataGrid 提供了使用这种分区的数据的方法。 DataGrid 通过 AllowCustomPaging 和 VirtualItemCount 属性支持块区操作。如果 AllowCustomPaging 为 true,则 DataGrid 不会根据 CurrentPageIndex 计算数据模型中的起始显示位置。DataGrid 将显示数据模型中的所有数据,而页导航栏将当前位置报告为 (VirtualItemCount+PageSize-1)/PageSize 之 CurrentPageIndex 页。下面的示例说明此功能。

    

 protected void BindDataGrid(int currpage)
 {
  string strConn = "Data Source=(local);Integrated Security=SSPI;database=Northwind";
  // 请确认 机器名/ASPNET 用户可以访问Northwind数据库
  SqlCommand cmd = new SqlCommand();
  SqlConnection conn = new SqlConnection(strConn);
  SqlParameter[]  parms = new SqlParameter[] {
   new SqlParameter("@PageSize",SqlDbType.Int),
   new SqlParameter("@CurrPage",SqlDbType.Int),
   new SqlParameter("@SearchSql",SqlDbType.NVarChar,128),
   new SqlParameter("@Count",SqlDbType.Int),
  };
  parms[0].Value = DataGrid1.PageSize;
  parms[1].Value = (currpage+1); 
  //  数据库的分页算法第一页是1  DataGrid的第一页是0
  parms[2].Value = DBNull.Value;
  parms[3].Direction = ParameterDirection.Output;
  parms[3].Value = DBNull.Value;
  DataSet DS = new DataSet();
  try 
  {
   if (conn.State != ConnectionState.Open) conn.Open();
   cmd.Connection = conn;
   cmd.CommandText = "Selected_Page_List";
   cmd.CommandType = CommandType.StoredProcedure;
   if (parms != null) 
   {
    foreach (SqlParameter parm in parms)
     cmd.Parameters.Add(parm);
   }
   SqlDataAdapter DA = new SqlDataAdapter(cmd);
   DA.Fill(DS);
   int aa = Convert.ToInt32(parms[3].Value.ToString());
   cmd.Parameters.Clear();
   if (currpage == 0)
   {
    DataGrid1.VirtualItemCount = aa;
   }
   DataGrid1.CurrentPageIndex = currpage;
   DataGrid1.DataSource = DS;
   DataGrid1.DataBind();
  }
  catch(Exception ewx)
  {
   conn.Close();
   Response.Write (ewx.Message.ToString());
   Response.End();
  }
 }

    void Page_Load(Object sender, EventArgs E ) {
  if (!IsPostBack) 
  {
   BindDataGrid(0);
   // 第一次打开这个页面,访问分页的第一页
  }
    }

    void MyDataGrid_Page(Object sender, DataGridPageChangedEventArgs e) {
  BindDataGrid(e.NewPageIndex);
    }

如果你有更多数据量的表稍加修改,也可以使用本演示程序。其下是演示代码下载,演示代码使用的是方案二。使用方法看readme.txt文件。

整个演示代码 下载

http://chs.gotdotnet.com/quickstart/aspplus/samples/webforms/ctrlref/webctrl/datagrid/doc_datagrid.aspx#paging
这里演示了利用DataGrid 的这个功能(没有本文中讨论的利用存储过程获得分区数据)。如对DataGrid的这个功能不太熟悉,请先看这里。

我最近碰到大数据量分页的问题,经过跟CSDN网友讨论,觉得比较可行的方案就是上面提到的2种方案,有谁有更好的方案,或者发现问题请Email联系我,thanks

我的Email是: ghj1976@csdn.net

 

 

复杂SQL查询通常会涉及到大的数据,为了提高性能和减少资源消耗,在处理大数据分页时可以考虑以下几个步骤: 1. 使用索引:在大数据分页查询中,使用适当的索引可以大大提高查询效率,减少数据库的读取数据。根据实际需求,合理创建索引可以有效减少查询所需的时间。 2. 分批查询:可以将大数据分页查询拆分为多个小的查询操作,每次查询一定数的数据。一个常见的做法是以主键为基础,每次查询一定数的记录,并利用主键范围进行数据的筛选。 3. 使用子查询:有些复杂查询可能需要多个子查询来获取相关数据,对于大数据的查询,可以在子查询中进行分页操作,这样可以减少对整个数据集的查询压力。 4. 利用缓存:对于热门查询结果,可以将查询结果缓存在缓存中,避免重复查询大数据的问题。这样可以显著提高查询性能。 5. 合理设置分页参数:根据具体需求和查询结果的大小,合理设置每页显示的数据和当前页码等分页参数。过大的每页数据可能会导致数据库负载过大,而过小的页码可能会增加对数据库的请求数。 需要注意的是,针对复杂SQL查询的大数据分页,不仅需要合理地设计SQL语句和数据库结构,还需要结合具体场景和需求,进行适当的调优和测试,并进行性能监控和优化。只有通过不断的调整和优化,才能获得更好的查询效率和用户体验。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值