sqlserver2005 use temporary tables and @ @RowCount improve paging query stored procedure performance sample sharing _mssql2005

Source: Internet
Author: User
Tags rowcount

The recent discovery of the performance of common query stored procedures for existing frameworks is slow, and the following code is carefully studied:

Copy Code code as follows:

Alter PROCEDURE [dbo]. [Areaselect]
@PageSize int=0,
@CurrentPage Int=1,
@Identifier Int=null,
@ParentId Int=null,
@AreaLevel Int=null,
@Children Int=null,
@AreaName nvarchar (m) =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 the 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 the 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

found that each query needs to query the area table by condition, the performance is too low, so the temporary table will be eligible to take out the records, and then for the temporary table query, the code modified as follows:
Alter PROCEDURE [dbo]. [Areaselect]
@PageSize int=0,
@CurrentPage Int=1,
@Identifier Int=null,
@ParentId Int=null,
@AreaLevel Int=null,
@Children Int=null,
@AreaName nvarchar (m) =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
--Create a temporary table
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

--Total
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 the 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

The use of temporary tables does improve performance, but there is a problem that count (Identifier) is very performance-intensive, and then it is modified

Alter PROCEDURE [dbo]. [Areaselect]
@PageSize int=0,
@CurrentPage Int=1,
@Identifier Int=null,
@ParentId Int=null,
@AreaLevel Int=null,
@Children Int=null,
@AreaName nvarchar (m) =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
--Number of records created
DECLARE @SumCount int

--Create a temporary table
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
--Set the total number of records to be just the number of record operations
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 the 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.