海量資料分頁查詢

2021-06-15 02:50:03 字數 3199 閱讀 2712

select top 25 id,registerid,filenameid,createtime from tbfilenamerecord where id < ( select min ( id ) from ( select top 100 id from tbfilenamerecord where registerid in (123,456,789) order by id desc ) as tbtmp ) and registerid in (123,456,789) order by id desc

select top 25 id,registerid,filenameid,createtime from tbfilenamerecord where id > ( select max ( id ) from ( select top 100 id from tbfilenamerecord where registerid in (123,456,789) order by id ) as tbtmp ) and registerid in (123,456,789) order by id

create procedure [dbo].[procedure_search_record]

@tablename varchar(256),    -- 表名

@returnfields varchar(1024) = '*',  -- 需要返回的列

@orderfields varchar(256)='',   -- 排序的欄位名

@pagesize int = 25,      -- 頁尺寸

@pageindex int = 1,      -- 頁碼

@returncount bit = 0,     -- 返回記錄總數, 非0值則返回

@ordertype bit = 0,      -- 設定排序型別, 非0值則降序

@wherefields varchar(1024) = ''   -- 查詢條件 (注意: 不要加 where)

asdeclare @sql varchar(4096)     -- 主語句

declare @tmp varchar(1024)     -- 臨時變數

declare @ord varchar(512)     -- 排序型別

if (@returncount <> 0)

begin

if (@wherefields <> '')

begin

set @sql = 'select count(*) as total from ' + @tablename + ' where ' + @wherefields

end else

set @sql = 'select count(*) as total from ' + @tablename

end

else begin

if (@ordertype <> 0)

begin

set @tmp = '< ( select min'

set @ord = ' order by ' + @orderfields +' desc'

end else begin

set @tmp = '> ( select max'

set @ord = ' order by ' + @orderfields

endif (@pageindex = 1)

begin

if (@wherefields <> '')

begin

set @sql = 'select top ' + str(@pagesize) + ' ' + @returnfields + ' from ' + @tablename + ' where ' + @wherefields + ' ' + @ord

end else begin

set @sql = 'select top ' + str(@pagesize) + ' ' + @returnfields + ' from ' + @tablename + ' '+ @ord

endend else begin   

if (@wherefields <> '')

begin

set @sql = 'select top ' + str(@pagesize) + ' ' + @returnfields + ' from ' + @tablename + ' where ' + @orderfields + ' ' + @tmp + ' ( ' + @orderfields + ' ) from ( select top ' + str( ( @pageindex - 1 ) * @pagesize ) + ' ' + @orderfields + ' from ' + @tablename + ' where ' + @wherefields + ' ' + @ord + ' ) as tbtmp ) and ' + @wherefields + ' ' + @ord    

end else begin

set @sql = 'select top ' + str(@pagesize) + ' ' + @returnfields + ' from ' + @tablename + ' where ' + @orderfields + ' ' + @tmp + ' ( ' + @orderfields + ' ) from ( select top ' + str( ( @pageindex - 1 ) * @pagesize ) + ' ' + @orderfields + ' from ' + @tablename + ' ' + @ord + ' ) as tbtmp ) '+ @ord

end  

end  

endprint @sql

exec (@sql)

declare @return_value int

exec @return_value = [dbo].[procedure_search_record]

@tablename = n'tbfilenamerecord',

@returnfields = n'id,registerid,filenameid,createtime',

@orderfields = n'id',

@pagesize = 25,

@pageindex = 5,

@returncount = 0,

@ordertype = 0,

@wherefields = n'registerid in (123,456,789)'

go

海量資料分頁

language vbscript codepage 936 分頁sql語句生成 function getpagesql tblname,fldname,pagesize,pageindex,ordertype,strwhere dim strtemp,strsql,strorder 根據排序方式生...

海量資料的分頁

第一種方法 declare pagesize int,currpage int,topnum int,previous int select pagesize 30 select currpage 2 select topnum currpage pagesize select previous c...

海量表資料之分頁查詢SQL優化

方案1 首先使用索引完成排序分頁操作,最後根據主鍵關聯回原表查詢所需要的其他列內容。1.普通分頁查詢 explain select film id,description from film order by title limit 50,5 掃瞄了全表,使用了filesort排序 速度很慢 2.改...