/* 名稱:spAll_ReturnRows 輸入: 輸出: 調(diào)用: EXEC spAll_ReturnRows 'select * FROM 表名', 頁號, 返回記錄數(shù), '主鍵', '排序字段' spAll_ReturnRows 'select * FROM all_Categories',2,10,'[ID]','[ID]' 說明:[百萬級]通用存儲過程.分頁存儲過程..返回指定返回條數(shù),、指定頁數(shù)的記錄 作者:Dili J.F. Senders 郵件:diliatwellknow.net 更新:20040610 版權(quán):轉(zhuǎn)述時請注明來源:用思維創(chuàng)造未來的Wellknow.net */
create PROCEDURE dbo.spAll_ReturnRows ( @SQL nVARCHAR(4000), @Page int, @RecsPerPage int, @ID VARCHAR(255), @Sort VARCHAR(255) ) AS
DECLARE @Str nVARCHAR(4000)
SET @Str='select TOP '+cast(@RecsPerPage AS VARCHAR(20))+' * FROM ('+@SQL+') T where T.'+@ID+' NOT IN (select TOP '+cast((@RecsPerPage*(@Page-1)) AS VARCHAR(20))+' '+@ID+' FROM ('+@SQL+') T9 ORDER BY '+@Sort+') ORDER BY '+@Sort
PRINT @Str
EXEC sp_ExecuteSql @Str GO
/* 名稱:spAll_deleteNoneUnique 輸入:要查詢的表名和字段列表 輸出: 調(diào)用: 說明:實現(xiàn)千萬級數(shù)據(jù)的分頁顯示!--可以在5秒內(nèi)獲取1448萬條記錄里的第1200頁的100條記錄,,雄不? 作者:鐵拳版權(quán):轉(zhuǎn)述時請注明來源:用思維創(chuàng)造未來的Wellknow.net */
create PROCEDURE GetRecordFromPage @tblName varchar(255), -- 表名 @fldName varchar(255), -- 字段名 @PageSize int = 10, -- 頁尺寸 @PageIndex int = 1, -- 頁碼 @IsCount bit = 0, -- 返回記錄總數(shù), 非 0 值則返回 @OrderType bit = 0, -- 設(shè)置排序類型, 非 0 值則降序 @strwhere varchar(1000) = '' -- 查詢條件 (注意: 不要加 where) AS
declare @strSQL varchar(6000) -- 主語句 declare @strTmp varchar(100) -- 臨時變量 declare @strOrder varchar(400) -- 排序類型
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(*) as Total from [" + @tblName + "]"
exec (@strSQL)
GO 個人研究百萬級數(shù)據(jù)提出問題總結(jié)如下: 1.除了提高硬件方面 2.采取折中方法,,一次提出幾百萬數(shù)據(jù)也沒有什么實在意義
不過存儲過程寫的還是很精妙。
p=pagesize,n為返回頁數(shù),SQL="select * from product where id<200000"
1.(select * from product where id<200000) as t 2.(select top p*(n-1) t.id from t order by id asc) as t1 3.select top p * from t where id not in t1 order by id asc
SQL: select top p * from (select * from product where id<200000) t where t.id not in (select top p*(n-1) id from t order by id asc) order by id asc [注:子查詢里面別名t 其實并不存在,,應(yīng)該用1.重新代替]
SQL存儲過程: SET @str='select top'+cast(@p as varchar(20))+' * from ('+@SQL+') t where t.'+@ID+' not in (select top '+cast((@p*(@n-1)) as varchar(20))+' '+@ID+' from ('+SQL+') order by '+@Sort+') order by '+@Sort
PRINT @str
百萬級數(shù)據(jù)測試結(jié)果如下: ////////////////////////////// ///result:100W數(shù)據(jù) /// ///條件:VirtualItemCount=200000 ///->轉(zhuǎn)1頁 33.924982085712 milliseconds,33.3696042374101 milliseconds,31.1483722093171 milliseconds ///->轉(zhuǎn)1000頁 247.380882207096 milliseconds,190.643224208663 milliseconds,240.085262233049 milliseconds ///->轉(zhuǎn)10000頁 2393.90201827327 milliseconds,1903.65535284512 milliseconds,1839.06278591273 milliseconds /// ///條件:VirtualItemCount=all ///->1 10.2540965402027 milliseconds,7.47608983823363 milliseconds,15.5991892824367 milliseconds ///->1000 273.380555349912 milliseconds,190.414982909839 milliseconds,217.308345055028 milliseconds ///->10000 1845.92064075183 milliseconds,1951.84974626663 milliseconds,1904.51831168487 milliseconds /// ///2s處理百萬數(shù)據(jù) ////////////////////////////// |