五種SQL Server分頁存儲進程的辦法及機能比擬。本站提示廣大學習愛好者:(五種SQL Server分頁存儲進程的辦法及機能比擬)文章只能為提供參考,不一定能成為您想要的結果。以下是五種SQL Server分頁存儲進程的辦法及機能比擬正文
在SQL Server數據庫操作中,我們經常會用到存儲進程對完成對查詢的數據的分頁處置,以便利閱讀者的閱讀。本文我們總結了五種SQL Server分頁存儲進程的辦法,並對其機能停止了比擬,接上去就讓我們來一路懂得一下這一進程。
創立數據庫data_Test :
create database data_Test GO use data_Test GO create table tb_TestTable --創立表 ( id int identity(1,1) primary key, userName nvarchar(20) not null, userPWD nvarchar(20) not null, userEmail nvarchar(40) null ) GO
拔出數據:
set identity_insert tb_TestTable on declare @count int set@count=1 while @count<=2000000 begin insert into tb_TestTable(id,userName,userPWD,userEmail) values(@count,'admin','admin888','[email protected]') set @count=@count+1 end set identity_insert tb_TestTable off
1、應用select top 和select not in停止分頁
詳細代碼以下:
create procedure proc_paged_with_notin --應用select top and select not in ( @pageIndex int, --頁索引 @pageSize int --每頁記載數 ) as begin set nocount on; declare @timediff datetime --耗時 declare @sql nvarchar(500) select @timediff=Getdate() set @sql='select top '+str(@pageSize)+' * from tb_TestTable where(ID not in(select top '+str(@pageSize*@pageIndex)+' id from tb_TestTable order by ID ASC)) order by ID' execute(@sql) --因select top後不支技直接接參數,所以寫成了字符串@sql select datediff(ms,@timediff,GetDate()) as 耗時 set nocount off; end
2、應用select top 和 select max(列鍵)
create procedure proc_paged_with_selectMax --應用select top and select max(列) ( @pageIndex int, --頁索引 @pageSize int --頁記載數 ) as begin set nocount on; declare @timediff datetime declare @sql nvarchar(500) select @timediff=Getdate() set @sql='select top '+str(@pageSize)+' * From tb_TestTable where(ID>(select max(id) From (select top '+str(@pageSize*@pageIndex)+' id From tb_TestTable order by ID) as TempTable)) order by ID' execute(@sql) select datediff(ms,@timediff,GetDate()) as 耗時 set nocount off; end
3、應用select top和中央變量
create procedure proc_paged_with_Midvar --應用ID>最年夜ID值和中央變量 ( @pageIndex int, @pageSize int ) as declare @count int declare @ID int declare @timediff datetime declare @sql nvarchar(500) begin set nocount on; select @count=0,@ID=0,@timediff=getdate() select @count=@count+1,@ID=case when @count<=@pageSize*@pageIndex then ID else @ID end from tb_testTable order by id set @sql='select top '+str(@pageSize)+' * from tb_testTable where ID>'+str(@ID) execute(@sql) select datediff(ms,@timediff,getdate()) as 耗時 set nocount off; end
4、應用Row_number() 此辦法為SQL server 2005中新的辦法,應用Row_number()給數據行加上索引
create procedure proc_paged_with_Rownumber --應用SQL 2005中的Row_number() ( @pageIndex int, @pageSize int ) as declare @timediff datetime begin set nocount on; select @timediff=getdate() select * from (select *,Row_number() over(order by ID asc) as IDRank from tb_testTable) as IDWithRowNumber where IDRank>@pageSize*@pageIndex and IDRank<@pageSize*(@pageIndex+1) select datediff(ms,@timediff,getdate()) as 耗時 set nocount off; end
5、應用暫時表及Row_number
create procedure proc_CTE --應用暫時表及Row_number ( @pageIndex int, --頁索引 @pageSize int --頁記載數 ) as set nocount on; declare @ctestr nvarchar(400) declare @strSql nvarchar(400) declare @datediff datetime begin select @datediff=GetDate() set @ctestr='with Table_CTE as (select ceiling((Row_number() over(order by ID ASC))/'+str(@pageSize)+') as page_num,* from tb_TestTable)'; set @strSql=@ctestr+' select * From Table_CTE where page_num='+str(@pageIndex) end begin execute sp_executesql @strSql select datediff(ms,@datediff,GetDate()) set nocount off; end
以上的五種辦法中,網上說第三種應用select top和中央變量的辦法是效力最高的。
關於SQL Server數據庫分頁的存儲進程的五種辦法及機能比擬的常識就引見到這裡了,願望對年夜家的進修有所贊助。