加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MsSql教程 > 正文

千万级数据分页详细设计

发布时间:2020-12-12 15:55:51 所属栏目:MsSql教程 来源:网络整理
导读:1 .引言 1 .1目的 为适应大数据量分页的需要,为以后千万级数据分页提供解决方法或者参考,节省开发时间,特制定本详细设计方案 1 .2主要阅读对象 脚本设计人员 1 .3参考资料 http: // www.cnblogs.com/SGSoft/archive/2004/10/23/55800.html 2 .详细设计 2

1 .引言

1 .1目的

为适应大数据量分页的需要,为以后千万级数据分页提供解决方法或者参考,节省开发时间,特制定本详细设计方案

1 .2主要阅读对象

脚本设计人员

1 .3参考资料

http:
// www.cnblogs.com/SGSoft/archive/2004/10/23/55800.html

2 .详细设计

2 .1分页存储过程

2.1 .1简介

2.1 .2分页存储过程代码

以下代码是网上找的分页存储过程,我是在原存储过程的基础上加了一个@IsCount?bit?
= ? 0 ,?主要是用来返回纪录总数,当为非0值时返回。下面注释部分是原作者的测试部分。我在本机sql?server?2005上的测试是在10000011纪录中查询第100000页,每页10条纪录按升序和降序时间均为0.38秒,测试语法如下:exec?GetRecordFromPage?tbl_Briefness,I_BriefnessID, 10 , 100000 ,其中在tbl_Briefness表I_BriefnessID字段上建立了索引。

/**/ /*

??经测试,在14483461?条记录中查询第100000?页,每页10?条记录按升序和降序第一次时间均为0.47?秒,第二次时间均为0.43?秒,测试语法如下:

??exec?GetRecordFromPage?news,newsid,10,100000

??news?为表名,?newsid?为关键字段,?使用时请先对newsid?建立索引。


??函数名称:?GetRecordFromPage

??函数功能:?获取指定页的数据

??参数说明:?@tblName??????包含数据的表名

???????????@fldName??????关键字段名

???????????@PageSize?????每页记录数

???????????@PageIndex????要获取的页码

???????????@OrderType????排序类型,?0?-?升序,?1?-?降序

???????????@strWhere?????查询条件(注意:?不要加where)

??创建时间:?2004-07-04

??修改时间:?2008-02-13

*/

ALTER?PROCEDURE?[dbo].[GetRecordFromPage]

????@tblName??????varchar(
255 ),??????? -- ?表名

????@fldName??????varchar(
255 ),??????? -- ?字段名

????@PageSize?????
int ? = ? 10 ,??????????? -- ?页尺寸

????@PageIndex????
int ? = ? 1 ,???????????? -- ?页码

????@OrderType????bit?
= ? 0 ,???????????? -- ?设置排序类型,?非0?值则降序

????@IsCount?bit?
= ? 0 ,? -- ?返回记录总数,?非0?值则返回

????@strWhere?????varchar(
2000 )? = ? '' ?? -- ?查询条件(注意:?不要加where)

????

AS


declare?@strSQL???varchar(
6000 )??????? -- ?主语句

declare?@strTmp???varchar(
1000 )??????? -- ?临时变量

declare?@strOrder?varchar(
500 )???????? -- ?排序类型


if ?@OrderType? != ? 0

begin

????
set ?@strTmp? = ? ' <(select?min '

????
set ?@strOrder? = ? ' ?order?by?[ ' ? + ?@fldName? + ? ' ]?desc '

end

else

begin

????
set ?@strTmp? = ? ' >(select?max '

????
set ?@strOrder? = ? ' ?order?by?[ ' ? + ?@fldName? + ' ]?asc '

end


set ?@strSQL? = ? ' select?top? ' ? + ?str(@PageSize)? + ? ' ?*?from?[ '

????
+ ?@tblName? + ? ' ]?where?[ ' ? + ?@fldName? + ? ' ] ' ? + ?@strTmp? + ? ' ([ '

????
+ ?@fldName? + ? ' ])?from?(select?top? ' ? + ?str((@PageIndex - 1 ) * @PageSize)? + ? ' ?[ '

????
+ ?@fldName? + ? ' ]?from?[ ' ? + ?@tblName? + ? ' ] ' ? + ?@strOrder? + ? ' )?as?tblTmp) '

????
+ ?@strOrder


if ?@strWhere? != ? ''

????
set ?@strSQL? = ? ' select?top? ' ? + ?str(@PageSize)? + ? ' ?*?from?[ '

????????
+ ?@tblName? + ? ' ]?where?[ ' ? + ?@fldName? + ? ' ] ' ? + ?@strTmp? + ? ' ([ '

????????
+ ?@fldName? + ? ' ])?from?(select?top? ' ? + ?str((@PageIndex - 1 ) * @PageSize)? + ? ' ?[ '

????????
+ ?@fldName? + ? ' ]?from?[ ' ? + ?@tblName? + ? ' ]?where? ' ? + ?@strWhere? + ? ' ? '

????????
+ ?@strOrder? + ? ' )?as?tblTmp)?and? ' ? + ?@strWhere? + ? ' ? ' ? + ?@strOrder


if ?@PageIndex? = ? 1

begin

????
set ?@strTmp? = ? ''

????
if ?@strWhere? != ? ''

????????
set ?@strTmp? = ? ' ?where?( ' ? + ?@strWhere? + ? ' ) '


????
set ?@strSQL? = ? ' select?top? ' ? + ?str(@PageSize)? + ? ' ?*?from?[ '

????????
+ ?@tblName? + ? ' ] ' ? + ?@strTmp? + ? ' ? ' ? + ?@strOrder

end

if ?@IsCount? != ? 0 ?

set ?@strSQL? = ? ' select?count( ' ? + ?@fldName? + ? ' )?as?Total?from?[ ' ? + ?@tblName? + ? ' ] ' ?

exec?(@strSQL)


??

2 .2分页控件的实现

2.2 .1分页控件的详细代码

using ?System;

using ?System.Collections.Generic;

using ?System.Text;

using ?System.ComponentModel;

using ?System.Web;

using ?System.Web.UI;

using ?System.Web.UI.WebControls;


namespace ?CustomControls

...
{

????[ToolboxData(
"<{0}:AspNetPager?runat='server'?PageSize='25'?FirstPageText='首页'?PrePageText='上一页'?NextPageText='下一页'?EndPageText='末页'?ButtonText='GO'></{0}:AspNetPager>")]

????
public?class?AspNetPager?:?WebControl,?INamingContainer

????...
{

????????属性块
属性块


????????分页事件相关
分页事件相关


????????样式属性
样式属性


????????自定义视图状态
自定义视图状态


????????生成控件
生成控件


????????按钮点击事件
按钮点击事件


????????重写TagKey
重写TagKey


????????绘制控件
绘制控件

????}

}


2 .3千万级数据分页实现

2.3 .1简介

这次分页我是用Gridview来实现的,测试时间没算,但基本上从10000011纪录中一次查询25条纪录,在10万页以内,时间花费?在1秒以内。使用其他控件比如DataGrid,DataList或者DataReapter应该花费的时间更短。


2.3 .2适用对象

服务器端控件Gridview,DataGrid,DataList,DataReapter等数据绑定控件


2.3 .3分页实现

分页效果图如下:



?


前台代码如下:


后台代码如下:


<% @?Page?Language = " C# " ?AutoEventWireup = " true " ?CodeFile = " Demo.aspx.cs " ?Inherits = " Demo " ? %>


<% @?Register?Assembly = " CustomControls " ?Namespace = " CustomControls " ?TagPrefix = " cc2 " ? %>


<% @?Register?Assembly = " MyLabel " ?Namespace = " MyLabel " ?TagPrefix = " cc1 " ? %>





<! DOCTYPE?html?PUBLIC? " -//W3C//DTD?XHTML?1.0?Transitional//EN " ? " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >


< html?xmlns = " http://www.w3.org/1999/xhtml " ? >

< head?runat = " server " >

????
< title > 无标题页 </ title >

?????
< link?type = " text/css " ?rel = " stylesheet " ?href = " css/comm.css " ? /> ?

</ head >

< body >

????
< form?id = " form1 " ?runat = " server " >

????
< div >

????????
& nbsp; & nbsp;

???????
< asp:GridView?ID = " GridView1 " ?runat = " server " ??AllowPaging = " false " ?AutoGenerateColumns = " False " ?CellPadding = " 4 " ?ForeColor = " #333333 " ?GridLines = " None " ?PagerSettings - Visible = " false "

????????????????????Width
= " 50% " ?height = " 35 " ???OnRowCommand = " GridView1_RowCommand " ?DataKeyNames = " I_BriefnessID " >

?????????
< FooterStyle?BackColor = " #507CD1 " ?Font - Bold = " True " ?ForeColor = " White " ? />

????????????????????
< RowStyle?BackColor = " #EFF3FB " ? />

????????????????????
< EditRowStyle?BackColor = " #2461BF " ? />

????????????????????
< SelectedRowStyle?BackColor = " #D1DDF1 " ?Font - Bold = " True " ?ForeColor = " #333333 " ? /> ??

????????????????????
< PagerStyle?ForeColor = " White " ?VerticalAlign = " Top " ?BackColor = " Transparent " ?? /> ?????????????????

????????????????????
< HeaderStyle?BackColor = " #507CD1 " ?Font - Bold = " True " ?ForeColor = " White " ? />

????????????????????
< AlternatingRowStyle?BackColor = " White " ? />

????????????????????
< Columns >

????????????????????????
< asp:TemplateField?HeaderText = " 序号 " >

????????????????????????????
< ItemTemplate >

????????????????????????????????
<% #?Container.DataItemIndex + 1 ? %>

????????????????????????????
</ ItemTemplate >

????????????????????????
</ asp:TemplateField >

????????????????????????
< asp:BoundField?DataField = " I_BriefnessID " ?HeaderText = " ID " ? />

????????????????????????
< asp:BoundField?DataField = " I_KMID " ?HeaderText = " 科目 " ? />

????????????????????????
< asp:BoundField?DataField = " C_Recno " ?HeaderText = " 试题号 " ? /> ?????????

????????????????????????
< asp:BoundField?DataField = " M_Title " ?HeaderText = " 题面 " ? />

????????????????????????
< asp:BoundField?DataField = " C_Answer " ?HeaderText = " 答案 " ? /> ??

??????????????????????????
< asp:TemplateField?HeaderText = " 删除 " >

????????????????????????????
< ItemTemplate > ?????????????????????????????????

???????????????????????????????????
< asp:LinkButton?ID = " LinkButton1 " ??OnClientClick = " return?confirm('确定要删除?'); " ?runat = " server "

????????????????????????????????????CausesValidation
= " False " ?CommandName = " DeleteData " ??CommandArgument = ' <%#DataBinder.Eval(Container.DataItem,"I_BriefnessID").ToString()%> ' ?Text = " 删除 " ? ></ asp:LinkButton >

????????????????????????????
</ ItemTemplate >

????????????????????????
</ asp:TemplateField >

????????????????????
</ Columns >

????????
</ asp:GridView >

????
</ div >

????
< div >

????????
< cc2:AspNetPager?ID = " AspNetPager1 " ?runat = " server " ?ButtonText = " GO " ?EndPageText = " 末页 "

????????????FirstPageText
= " 首页 " ?NextPageText = " 下一页 " ?PageSize = " 15 " ?PrePageText = " 上一页 " ??OnPageChanged = " Page_Changed " ?Width = " 50% " >

????????????
< ButtonStyle?CssClass = " btn1_mouSEOut " ?Width = " 30px " ? />

????????????
< TextBoxStyle?Width = " 30px " ?CssClass = " blue_rounded " />

????????????
< LabelStyle?ForeColor = " red " ??Font - Bold = " true " ? />

????????
</ cc2:AspNetPager >

????????
& nbsp; & nbsp;

????????
</ div >

????
</ form >

</ body >

</ html >


?


?


using ?System;

using ?System.Data;

using ?System.Configuration;

using ?System.Collections;

using ?System.Web;

using ?System.Web.Security;

using ?System.Web.UI;

using ?System.Web.UI.WebControls;

using ?System.Web.UI.WebControls.WebParts;

using ?System.Web.UI.HtmlControls;


public ? partial ? class ?Demo?:?System.Web.UI.Page

...
{

????
protected?void?Page_Load(object?sender,?EventArgs?e)

????...
{

????????
if?(!Page.IsPostBack)

????????...
{

????????????TestDataCount();

????????????BindPaperDefineProgramme(
1);

????????}

????????

????}


????绑定试卷定义方案列表
绑定试卷定义方案列表


????
protected?void?Page_Changed(object?sender,?EventArgs?e)

????...
{

????????BindPaperDefineProgramme(AspNetPager1.PageIndex);

????}


????删除纪录
删除纪录


????绑定试卷定义方案列表
绑定试卷定义方案列表

}

?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读