sqlserver2005利用臨時(shí)表和@@RowCount提高分頁(yè)查詢(xún)存儲(chǔ)過(guò)程性能示例分享
最近發(fā)現(xiàn)現(xiàn)有框架的通用查詢(xún)存儲(chǔ)過(guò)程的性能慢,于是仔細(xì)研究了下代碼:
Alter PROCEDURE [dbo].[AreaSelect]
@PageSize int=0,
@CurrentPage int=1,
@Identifier int=NULL,
@ParentId int=NULL,
@AreaLevel int=NULL,
@Children int=NULL,
@AreaName nvarchar(50)=NULL,
@Path nvarchar(MAX)=NULL,
@Status int=NULL,
@Alt int=NULL
AS
BEGIN
SET NOCOUNT ON;
IF (NOT @AreaName IS NULL) SET @AreaName='%'+@AreaName+'%'
IF (NOT @Path IS NULL) SET @Path='%'+@Path+'%'
IF (@PageSize>0)
BEGIN
DECLARE @TotalPage int
Select @TotalPage=Count(Identifier) FROM Area Where
(@Identifier IS NULL or Identifier=@Identifier)AND
(@ParentId IS NULL or ParentId=@ParentId)AND
(@AreaLevel IS NULL or AreaLevel=@AreaLevel)AND
(@Children IS NULL or Children=@Children)AND
(@AreaName IS NULL or AreaName Like @AreaName)AND
(@Path IS NULL or Path Like @Path)AND
(@Status IS NULL or Status=@Status)AND
(@Alt IS NULL or Alt=@Alt)
IF(@TotalPage%@PageSize=0)
BEGIN
SET @TotalPage=@TotalPage/@PageSize
END
ELSE
BEGIN
SET @TotalPage=Round(@TotalPage/@PageSize,0)+1
END
Select TOP (@PageSize) Identifier,ParentId,AreaLevel,Children,AreaName,Path,Status,Alt,@TotalPage as totalPage FROM Area Where
Identifier NOT IN (Select Top (@PageSize*(@CurrentPage-1))Identifier FROM Area Where
(@Identifier IS NULL or Identifier=@Identifier)AND
(@ParentId IS NULL or ParentId=@ParentId)AND
(@AreaLevel IS NULL or AreaLevel=@AreaLevel)AND
(@Children IS NULL or Children=@Children)AND
(@AreaName IS NULL or AreaName Like @AreaName)AND
(@Path IS NULL or Path Like @Path)AND
(@Status IS NULL or Status=@Status)AND
(@Alt IS NULL or Alt=@Alt)
order by AreaName asc)
AND
(@Identifier IS NULL or Identifier=@Identifier)AND
(@ParentId IS NULL or ParentId=@ParentId)AND
(@AreaLevel IS NULL or AreaLevel=@AreaLevel)AND
(@Children IS NULL or Children=@Children)AND
(@AreaName IS NULL or AreaName Like @AreaName)AND
(@Path IS NULL or Path Like @Path)AND
(@Status IS NULL or Status=@Status)AND
(@Alt IS NULL or Alt=@Alt)
order by AreaName asc
END
ELSE
BEGIN
Select Identifier,ParentId,AreaLevel,Children,AreaName,Path,Status,Alt FROM Area Where
(@Identifier IS NULL or Identifier=@Identifier)AND
(@ParentId IS NULL or ParentId=@ParentId)AND
(@AreaLevel IS NULL or AreaLevel=@AreaLevel)AND
(@Children IS NULL or Children=@Children)AND
(@AreaName IS NULL or AreaName Like @AreaName)AND
(@Path IS NULL or Path Like @Path)AND
(@Status IS NULL or Status=@Status)AND
(@Alt IS NULL or Alt=@Alt)
order by AreaName asc
END
END
發(fā)現(xiàn)每次查詢(xún)都需要按條件查詢(xún)依次Area表,性能太低,于是利用臨時(shí)表將符合條件的記錄取出來(lái),然后針對(duì)臨時(shí)表進(jìn)行查詢(xún),代碼修改如下:
Alter PROCEDURE [dbo].[AreaSelect]
@PageSize int=0,
@CurrentPage int=1,
@Identifier int=NULL,
@ParentId int=NULL,
@AreaLevel int=NULL,
@Children int=NULL,
@AreaName nvarchar(50)=NULL,
@Path nvarchar(MAX)=NULL,
@Status int=NULL,
@Alt int=NULL
AS
BEGIN
SET NOCOUNT ON;
IF (NOT @AreaName IS NULL) SET @AreaName='%'+@AreaName+'%'
IF (NOT @Path IS NULL) SET @Path='%'+@Path+'%'
IF (@PageSize>0)
BEGIN
--創(chuàng)建臨時(shí)表
Select
Identifier,ParentId,AreaLevel,Children,AreaName,Path,Status,Alt
INTO #temp_Area
FROM Area Where
(@Identifier IS NULL or Identifier=@Identifier)AND
(@ParentId IS NULL or ParentId=@ParentId)AND
(@AreaLevel IS NULL or AreaLevel=@AreaLevel)AND
(@Children IS NULL or Children=@Children)AND
(@AreaName IS NULL or AreaName Like @AreaName)AND
(@Path IS NULL or Path Like @Path)AND
(@Status IS NULL or Status=@Status)AND
(@Alt IS NULL or Alt=@Alt)
order by AreaName asc
DECLARE @TotalPage int
DECLARE @SumCount int
--取總數(shù)
Select @SumCount=Count(Identifier) FROM #temp_Area
IF(@SumCount%@PageSize=0)
BEGIN
SET @TotalPage=@SumCount/@PageSize
END
ELSE
BEGIN
SET @TotalPage=Round(@SumCount/@PageSize,0)+1
END
Select TOP (@PageSize) Identifier,ParentId,AreaLevel,Children,AreaName,
Path,Status,Alt,@TotalPage as totalPage,@SumCount as SumCount
FROM #temp_Area
Where
Identifier NOT IN (Select Top (@PageSize*(@CurrentPage-1))Identifier FROM #temp_Area))
END
ELSE
BEGIN
Select Identifier,ParentId,AreaLevel,Children,AreaName,Path,Status,Alt FROM Area Where
(@Identifier IS NULL or Identifier=@Identifier)AND
(@ParentId IS NULL or ParentId=@ParentId)AND
(@AreaLevel IS NULL or AreaLevel=@AreaLevel)AND
(@Children IS NULL or Children=@Children)AND
(@AreaName IS NULL or AreaName Like @AreaName)AND
(@Path IS NULL or Path Like @Path)AND
(@Status IS NULL or Status=@Status)AND
(@Alt IS NULL or Alt=@Alt)
order by AreaName asc
END
END
經(jīng)過(guò)使用臨時(shí)表的確提高性能,不過(guò)有發(fā)現(xiàn)一個(gè)問(wèn)題,就是count(Identifier)的確很耗性能,于是又進(jìn)行修改了
:
Alter PROCEDURE [dbo].[AreaSelect]
@PageSize int=0,
@CurrentPage int=1,
@Identifier int=NULL,
@ParentId int=NULL,
@AreaLevel int=NULL,
@Children int=NULL,
@AreaName nvarchar(50)=NULL,
@Path nvarchar(MAX)=NULL,
@Status int=NULL,
@Alt int=NULL
AS
BEGIN
SET NOCOUNT ON;
IF (NOT @AreaName IS NULL) SET @AreaName='%'+@AreaName+'%'
IF (NOT @Path IS NULL) SET @Path='%'+@Path+'%'
IF (@PageSize>0)
BEGIN
--創(chuàng)建中記錄數(shù)
DECLARE @SumCount int
--創(chuàng)建臨時(shí)表
Select
Identifier,ParentId,AreaLevel,Children,AreaName,Path,Status,Alt
INTO #temp_Area
FROM Area Where
(@Identifier IS NULL or Identifier=@Identifier)AND
(@ParentId IS NULL or ParentId=@ParentId)AND
(@AreaLevel IS NULL or AreaLevel=@AreaLevel)AND
(@Children IS NULL or Children=@Children)AND
(@AreaName IS NULL or AreaName Like @AreaName)AND
(@Path IS NULL or Path Like @Path)AND
(@Status IS NULL or Status=@Status)AND
(@Alt IS NULL or Alt=@Alt)
order by AreaName asc
--設(shè)置總記錄數(shù)為剛操作的記錄數(shù)
SET @SumCount=@@RowCount
DECLARE @TotalPage int
IF(@SumCount%@PageSize=0)
BEGIN
SET @TotalPage=@SumCount/@PageSize
END
ELSE
BEGIN
SET @TotalPage=Round(@SumCount/@PageSize,0)+1
END
Select TOP (@PageSize) Identifier,ParentId,AreaLevel,Children,AreaName,
Path,Status,Alt,@TotalPage as totalPage,@SumCount as SumCount
FROM #temp_Area
Where
Identifier NOT IN (Select Top (@PageSize*(@CurrentPage-1))Identifier FROM #temp_Area))
END
ELSE
BEGIN
Select Identifier,ParentId,AreaLevel,Children,AreaName,Path,Status,Alt FROM Area Where
(@Identifier IS NULL or Identifier=@Identifier)AND
(@ParentId IS NULL or ParentId=@ParentId)AND
(@AreaLevel IS NULL or AreaLevel=@AreaLevel)AND
(@Children IS NULL or Children=@Children)AND
(@AreaName IS NULL or AreaName Like @AreaName)AND
(@Path IS NULL or Path Like @Path)AND
(@Status IS NULL or Status=@Status)AND
(@Alt IS NULL or Alt=@Alt)
order by AreaName asc
END
END
- sqlserver給表添加新字段、給表和字段添加備注、更新備注及查詢(xún)備注(sql語(yǔ)句)
- 使用SQL語(yǔ)句查詢(xún)MySQL,SQLServer,Oracle所有數(shù)據(jù)庫(kù)名和表名,字段名
- 詳解sqlserver查詢(xún)表索引
- SQLServer中用T—SQL命令查詢(xún)一個(gè)數(shù)據(jù)庫(kù)中有哪些表的sql語(yǔ)句
- SQLSERVER查詢(xún)所有數(shù)據(jù)庫(kù)名,表名,和字段名的語(yǔ)句
- mysql和sqlserver查詢(xún)數(shù)據(jù)庫(kù)表的數(shù)量方法詳解
相關(guān)文章
讓sql2005運(yùn)行在獨(dú)立用戶(hù)下出現(xiàn) WMI 提供程序錯(cuò)誤的解決方式
您還有 %1 的時(shí)間注銷(xiāo),如果您 在此時(shí)間到之后仍沒(méi)有注銷(xiāo),您的會(huì)話 將會(huì)斷開(kāi),任何打開(kāi)的文件或設(shè)備 就可能丟失數(shù)據(jù)2012-11-11
SQL Server 2005 模板參數(shù)使用說(shuō)明
如果你用SQL Server 2005 Management Studio建立函數(shù)或存儲(chǔ)過(guò)程,你會(huì)注意到這些新窗口中都是模板。通常,你可以獲得一個(gè)散布著標(biāo)記的框架。2009-01-01
SQL Server 2005 開(kāi)啟數(shù)據(jù)庫(kù)遠(yuǎn)程連接的方法
這篇文章主要介紹了SQL Server 2005默認(rèn)是不允許遠(yuǎn)程連接的,要想通過(guò)遠(yuǎn)程連接實(shí)現(xiàn)MSSQL,數(shù)據(jù)庫(kù)備份,需要做如下設(shè)置,需要的朋友可以參考下2015-01-01
SQLServer2005 批量查詢(xún)自定義對(duì)象腳本
使用系統(tǒng)函數(shù)object_definition和系統(tǒng)表 sysobjects 就可以了2009-08-08
sqlserver巧用row_number和partition by分組取top數(shù)據(jù)
SQL Server 2005后之后,引入了row_number()函數(shù),row_number()函數(shù)的分組排序功能使這種操作變得非常簡(jiǎn)單2011-12-12
SQL?Server中帶有OUTPUT子句的INSERT,DELETE,UPDATE應(yīng)用
這篇文章介紹了SQL?Server中帶有OUTPUT子句的INSERT,DELETE,UPDATE應(yīng)用,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06
SQLServer2005與SQLServer2008數(shù)據(jù)庫(kù)同步圖文教程
要實(shí)現(xiàn)SQLServer2005與2005的數(shù)據(jù)庫(kù)同步的話,直接用鏡像就可以實(shí)現(xiàn)。但是如果同步 SQLServer2008的話,2005的實(shí)例是連接不上08的。低版本的無(wú)法連接高版本的。所以我們可以通過(guò)復(fù)制的方式,也就是所謂的訂閱發(fā)布的方法來(lái)實(shí)現(xiàn)兩個(gè)不同版本數(shù)據(jù)庫(kù)的數(shù)據(jù)同步。2011-09-09
Sql Server 2005的1433端口打開(kāi)局域網(wǎng)訪問(wèn)和進(jìn)行遠(yuǎn)程連接
在實(shí)際項(xiàng)目中,我們經(jīng)常會(huì)遇到需要局域網(wǎng)訪問(wèn)或者需要外網(wǎng)訪問(wèn)甚至是兩者都需要的數(shù)據(jù)庫(kù)搭建,那么應(yīng)該如何來(lái)處理呢,我們來(lái)一一探討下2014-08-08

