濮阳杆衣贸易有限公司

主頁(yè) > 知識(shí)庫(kù) > sqlserver2005利用臨時(shí)表和@@RowCount提高分頁(yè)查詢存儲(chǔ)過(guò)程性能示例分享

sqlserver2005利用臨時(shí)表和@@RowCount提高分頁(yè)查詢存儲(chǔ)過(guò)程性能示例分享

熱門標(biāo)簽:海南自動(dòng)外呼系統(tǒng)價(jià)格 松原導(dǎo)航地圖標(biāo)注 九鹿林外呼系統(tǒng)怎么收費(fèi) 滄州營(yíng)銷外呼系統(tǒng)軟件 沈陽(yáng)智能外呼系統(tǒng)代理 創(chuàng)業(yè)電銷機(jī)器人 浙江地圖標(biāo)注 電銷機(jī)器人虛擬號(hào)碼 舞鋼市地圖標(biāo)注app

最近發(fā)現(xiàn)現(xiàn)有框架的通用查詢存儲(chǔ)過(guò)程的性能慢,于是仔細(xì)研究了下代碼:

復(fù)制代碼 代碼如下:

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)每次查詢都需要按條件查詢依次Area表,性能太低,于是利用臨時(shí)表將符合條件的記錄取出來(lái),然后針對(duì)臨時(shí)表進(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í)表
        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

您可能感興趣的文章:
  • sql server2008調(diào)試存儲(chǔ)過(guò)程的完整步驟
  • SQLServer2008存儲(chǔ)過(guò)程實(shí)現(xiàn)數(shù)據(jù)插入與更新
  • Sql Server 存儲(chǔ)過(guò)程調(diào)用存儲(chǔ)過(guò)程接收輸出參數(shù)返回值
  • SQLServer存儲(chǔ)過(guò)程創(chuàng)建和修改的實(shí)現(xiàn)代碼
  • 獲取SqlServer存儲(chǔ)過(guò)程定義的三種方法
  • SqlServer存儲(chǔ)過(guò)程實(shí)現(xiàn)及拼接sql的注意點(diǎn)
  • 淺析SQL Server的嵌套存儲(chǔ)過(guò)程中使用同名的臨時(shí)表怪像

標(biāo)簽:寶雞 咸寧 商洛 ???/a> 臺(tái)灣 西藏 日喀則 公主嶺

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《sqlserver2005利用臨時(shí)表和@@RowCount提高分頁(yè)查詢存儲(chǔ)過(guò)程性能示例分享》,本文關(guān)鍵詞  sqlserver2005,利用,臨時(shí),表,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《sqlserver2005利用臨時(shí)表和@@RowCount提高分頁(yè)查詢存儲(chǔ)過(guò)程性能示例分享》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于sqlserver2005利用臨時(shí)表和@@RowCount提高分頁(yè)查詢存儲(chǔ)過(guò)程性能示例分享的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    江城| 曲周县| 家居| 湄潭县| 荃湾区| 高雄市| 会理县| 武胜县| 辉南县| 稻城县| 通化市| 麻栗坡县| 博兴县| 阳原县| 齐河县| 略阳县| 昌乐县| 施甸县| 财经| 云霄县| 油尖旺区| 隆子县| 罗定市| 施甸县| 乳山市| 吉水县| 武城县| 浦城县| 于田县| 怀来县| 濮阳县| 图片| 武城县| 嘉义市| 临武县| 崇礼县| 柘城县| 佛教| 林西县| 永城市| 元朗区|