對于非常大的數據模型而言,分頁檢索時,每次都加載整個數據源非常浪費。通常的選擇是檢索頁面大小的塊區的數據,而非檢索所有的數據,然后單步執行當前行。
本文演示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是: ghj197
|