真分页核心Sql语句

dropprocedure P_PageSizer
go
createprocedure P_Pagesizer
(
@startindexint,
@endindexint
)
as
declare@numint
declare@sqlnvarchar(2000)
begin
set@num=@endindex-@startindex;
set@sql='select top '+Convert(nvarchar(10),@num)+' * from T_Results where r_id not in (select top '+Convert(nvarchar(10),@num)+' r_id from T_Results)';
--set @sql = 'select top '+str(@num)+' * from T_Results where r_id not in (select top '+str(@num)+' r_id from T_Results)';
exec(@sql)
end
假分页实现: a.添加AspNetPager引用 b.在aspx文件中添加文件头 c. 在aspx文件中需要分页的控件(如GridView)后添加代码 d.在cs文件中修改数据绑定方法DataBind(string condition) e.在cs文件中添加分页事件
b.在aspx文件中添加文件头
<%@ Register Assembly="AspNetPager" Namespace="Wuqi.Webdiyer" TagPrefix="webdiyer"%>
c. 在aspx文件中需要分页的控件(如GridView)后添加代码

<table border="0" cellpadding="0" cellspacing="0" width="75%" class="tb_2_tl" style="text-align: right">
<tr>
<td align="right">
<webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="True" CssClass="menu2"
CustomInfoSectionWidth="" FirstPageText="首页" HorizontalAlign="Justify" LastPageText="末页"
NavigationToolTipTextFormatString="转到第{0}页" NextPageText="下页" NumericButtonTextFormatString="[{0}]"
OnPageChanged="AspNetPager1_PageChanged" PagingButtonSpacing="8px" PrevPageText="上页"
ShowBoxThreshold="2" ShowNavigationToolTip="True" SubmitButtonText="GO" Width="10px"
SubmitButtonStyle="BACKGROUND-COLOR: #d6e8ff;30px;height:20px" PageIndexBoxStyle="30px;height:13px; text-align:center;bottom:2px"
Wrap="False" PageSize="5">
</webdiyer:AspNetPager>
</td>
<td align="center">
<asp:Label ID="lblCustom" runat="server" CssClass="menu2"></asp:Label>
</td>
</tr>
</table>
d.在cs文件中修改数据绑定方法DataBind(string condition)

{
System.Data.DataSet ds = u.SelectData(conditions);//第一次执行一次查询, 获得数据库中的所有数据
//从数据库中获取的数据, 一定要做非空判断. 否则会有太多黄页
if (ds !=null&& ds.Tables[0].Rows.Count !=0)//数据集System.Data.DataSet内部有多张表, 假设有3张表. 当使用SqlDataAdapter时, 将使用存储过程查询3张表, 并填入3张表中. 而这个提里面, 只查了一个表, 所以只有一张表DataSet.Tables[0]
{
//使用分页控件
//分页控件的数据源
System.Data.DataView dv = ds.Tables[0].DefaultView;//Ds中, Tables[0]的默认视图, 相当于一个快照
AspNetPager1.RecordCount = dv.Count;//这只分页控件的行数, 记得这里是RecordCount
//分页数据源
PagedDataSource pds =new PagedDataSource();
pds.DataSource = dv;
//分页控件的设置
pds.AllowPaging =true;
pds.PageSize = AspNetPager1.PageSize;
pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex -1;//因为分页控件中的数据是从1开的是, 而数据源是从0开始的, 所以需要-1
//设置GridView的数据源
this.GridView_UserInfo.DataSource = pds;
this.GridView_UserInfo.DataBind();
//不使用分页控件时
//this.GridView_UserInfo.DataSource = ds;//在GridView编辑列时, 要去掉自动生成列, 这样才能清空原来显示的内容
//this.GridView_UserInfo.DataBind();
}
else
{
this.GridView_UserInfo.DataSource =null;
this.GridView_UserInfo.DataBind();
}
}
e.在cs文件中添加分页事件
{
BindData("");
}
//转载:
举例插入自增长的主键列:
--ALTER table E1T04_2006_TEMP drop column ID
--ALTER table E1T04_2006_TEMP add ID int identity(1,1)
--ALTER TABLE E1T04_2006_TEMP ADD primary key(ID)
----------------
分页查询效率比较结论:
在小数据量下(一般应该认为是10万以下,TOP+NOT IN分页方式效率要比ROW_NUMBER()高;在大数据量下(百万级)ROW_NUMBER()分页方式效率要更高一些。
A.首先插入100万条测试数据:
user Stock_new
go
declare @index int
set @index=0
while @index<1000000
begin
insert into Users(sno,[Name]) values(@index,'TEST')
set @index=@index+1
end
-----
B.接下来先扫盲一下ROW_NUMBER()函数。
ROW_NUMBER()函数是根据参数传递过来的order by子句的值,返回一个不断递增的整数值,也就是它会从1一直不断自增1,直到条件不再满足。例如表Users(Id,Name),使用以下sql语句进行查询:
- select id,name,row_number() over(order by Id desc) as rowNum from users where id<10
- select id,name,row_number() over(order by Id) as rowNum from users where id<10
select id,name,row_number() over(order by Id desc) as rowNum from users where id<10 select id,name,row_number() over(order by Id) as rowNum from users where id<10
两条语句order by排序相反.
C.孰优孰劣
以下两种情况,同样取500000到500100中间的数据。
1.row_number() over(),语句如下:
declare @time datetime
declare @ms int
set @time= getdate()
select sno,[Name] from (select row_number() over(order by sno) as rowNum,* from T_leamon) as t where rowNum between 500000 and 500100
set @ms=datediff(ms,@time,getdate())
print @ms--毫秒数
2.使用TOP加NOT IN方法,语句如下:
declare @time datetime
declare @ms int
set @time= getdate()
select top 100 * from T_leamon where sno not in
(select top 500000 sno from T_leamon order by sno)
order by sno
set @ms=datediff(ms,@time,getdate())
print @ms--毫秒数