SQLServer 2012 高效分頁

2022-03-09 08:33:24 字數 3355 閱讀 5305

sql code

/*

功能:生成測試資料.*/

create table test_paging(

id int identity(1,1) not null primary key,

testnumber int not null,

testname varchar(20) not null,

testdept varchar(10) not null,

testdate datetime not null)go

with tep(number,name,dept,date) as

( select 1,cast('0_testname' as varchar(20)),cast('0_dba' as varchar(10)),getdate()

union all

select number+1,cast(cast(number as varchar(20))+'_testname' as varchar(20)),cast(cast(number/500 as varchar(10))+'_dba' as varchar(10)) ,getdate()

from tep

where number<=20000000

)insert into test_paging(testnumber,testname,testdept,testdate)

select number,name,dept,date from tep option(maxrecursion 0)

--新增索引(我有測試沒有索引的情況,2012的優勢更加明顯,但是我們的資料庫不可能不建索引的,故可忽略沒有索引的情況)

create nonclustered index ix_testdept on test_paging(

testdept

) include

( testname,testdate

) go

sql code

/*

功能:測試2012版本中offset/fetch分頁.*/

dbcc dropcleanbuffers

dbcc freeproccache

set statistics io on

set statistics time on

set statistics profile on

declare

@page int, --第@page頁

@size int, --每頁@size行

@total int --總行數

select @page=20,@size=10,@total=count(1) from test_paging where testdept = '1000_dba'

select

testname,testdept,testdate,@total

from

test_paging

where

testdept = '1000_dba'

order by id offset (@page-1)*@size rows fetch next @size rows only

set statistics io off

set statistics time off

set statistics profile off

sql code

/*

功能:測試2005/2008版本中row_number分頁.*/

dbcc dropcleanbuffers

dbcc freeproccache

set statistics io on

set statistics time on

set statistics profile on

declare

@page int, --第@page頁

@size int, --每頁@size行

@total int

select @page=20,@size=10,@total=count(1) from test_paging where testdept = '1000_dba'

select testname,testdept,testdate,@total from

( select

testname,testdept,testdate,row_number() over(order by id) as num

from

test_paging

where

testdept = '1000_dba'

) test where num between (@page-1)*@size+1 and @page*@size order by num

set statistics io off

set statistics time off

set statistics profile off

sql code

/*

功能:測試2000版本中top分頁.*/

dbcc dropcleanbuffers

dbcc freeproccache

set statistics io on

set statistics time on

set statistics profile on

declare

@page int, --第@page頁

@size int, --每頁@size行

@total int --總行數

select @page=20,@size=10,@total=count(1) from test_paging where testdept = '1000_dba'

select testname,testdept,testdate,@total from

( select top(@size) id,testname,testdept,testdate from

(select top(@page*@size) id,testname,testdept,testdate

from test_paging

where testdept = '1000_dba'

order by id

)temp1 order by id desc

)temp2 order by id

set statistics io off

set statistics time off

set statistics profile off

原文:

解除安裝sql server 2012

好不容易裝上了sql server2012資料庫,可是卻不能連線本地的資料庫,後來發現缺少一些服務,於是決定重新安裝,但是解除安裝卻很麻煩,如果解除安裝不乾淨的話,重新安裝會出問題,所以下面就總結一些方法 在解除安裝sql server 2012後,大家都希望能夠將登錄檔資訊完全刪乾淨,下面就將教您...

SQL Server2012中的Throw語句

簡 介sql server2012實現了 類似c 丟擲異常的 throw 語句。相比較於 sqlserver2005 之前使用 error,和sqlserver2005 之後使用 raiserror 引發 異常都是乙個不小的 進步,下面來看一下 throw 的用法。raiserror 和throw ...

sql server2012 遠端訪問設定

開啟sql server2012,使用windows身份登入 登入後,右鍵選擇 屬性 左側選擇 安全性 選中右側的 sql server 和 windows 身份驗證模式 以啟用混合登入模式 選擇 連線 勾選 允許遠端連線此伺服器 然後點 確定 展開 安全性 登入名 sa 右鍵選擇 屬性 左側選擇 ...