Sql Query需要三倍的时间:

时间:2015-05-15 04:56:56

标签: sql-server vb.net

修改

我已更改正在使用的存储过程。这个新查询会跳过检查结果的“有库存”列的部分(因为这在其他地方更容易实现),而只检查“已提交”库存。

USE [PCSQL]
GO
/****** Object:  StoredProcedure [dbo].[xw_GetAvailableStockLevel]    Script Date: 05/18/2015 09:50:32 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER OFF
GO

ALTER PROCEDURE [dbo].[xw_GetAvailableStockLevel] 
    @ProductID nvarchar(128)

AS

DECLARE @TotalCommited int
SET @TotalCommited = 0

SELECT @TotalCommited = SUM(Unsup_quant)  

FROM SALESORD_LINES 

WHERE DATEDIFF(MONTH,DUEDATE,GETDATE()) < 3 

AND Location IN('4','12') AND STOCKCODE=@ProductID AND Hdr_Status < 2

SELECT isnull(@TotalCommited,0) as TotalCommited

根据项目数量,每页的加载时间从大约40秒急剧下降到大约8-15秒。

我现在的问题是,此查询对于管理来说仍然“太慢”。您是否认为可以进一步简化此查询?

我只尝试过检查过去三个月的记录,但我不知道它会有多大用处。在过去的5年中,有超过250,000个表行偶尔追溯。

结束编辑

我试图更新网站的一小部分,包括检查产品是否有库存。

我已从一个调用两个SQL查询并使用额外数据编译结果的大型函数更改, 一个较小的函数,调用稍微改变的查询返回较少的数据...我的页面加载时间增加了一倍多?

我的新功能实际上更复杂吗?如何加快页面加载速度?

有问题的查询:

原创(VB.net调用两个存储过程)

Public Shared Function GetStockLevelsComplete(ByVal strProductCode As String) As String
        Dim strOutput As String = "<div class=""product-column-right-location"">&nbsp;</div>" & _
        "<div class=""product-column-right-stock""><strong>Stock</strong></div>" & _
        "<div class=""product-column-right-committed""><strong>Committed</strong></div>" & _
        "<div class=""product-column-right-on-order""><strong>On Order</strong></div>" & _
        "<div class=""product-column-right-available""><strong>Available</strong></div>"
        Dim dt As DataTable = New DataTable
        'Dim dt As DataTable = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("dbConn"), "xw_GetStockLevelComplete", strProductCode, 1).Tables(0)
        'If Not dt Is Nothing Then
        '    If dt.Rows.Count > 0 Then
        '        Dim dr As DataRow = dt.Rows(0)
        '        strOutput += "<tr>" & _
        '        "<td>St Asaph</td>" & _
        '        "<td>" & dr("TotalInStock") & "</td>" & _
        '        "<td>" & dr("TotalCommited") & "</td>" & _
        '        "<td>" & dr("TotalOnOrder") & "</td>" & _
        '        "<td>" & (dr("TotalInStock") - dr("TotalCommited")) & "</td>" & _
        '        "</tr>"
        '    End If
        'End If
        dt = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("dbConn"), "xw_GetStockLevelComplete", strProductCode, 4).Tables(0)
        If Not dt Is Nothing Then
            If dt.Rows.Count > 0 Then
                Dim dr As DataRow = dt.Rows(0)
                strOutput += "<div class=""product-column-right-location""><a href=""/contact-us.aspx"" alt=""Global PC Tower Junction contact information"" Title=""Global PC Tower Junction contact information"">Tower Junction - Riccarton</a></div>" & _
                "<div class=""product-column-right-stock"">" & dr("TotalInStock") & "</div>" & _
                "<div class=""product-column-right-committed"">" & dr("TotalCommited") & "</div>" & _
                "<div class=""product-column-right-on-order"">" & dr("TotalOnOrder") & "</div>" & _
                "<div class=""product-column-right-available"">" & (dr("TotalInStock") - dr("TotalCommited")) & "</div>"
            End If
        End If
        dt = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("dbConn"), "xw_GetStockLevelComplete", strProductCode, 12).Tables(0)
        If Not dt Is Nothing Then
            If dt.Rows.Count > 0 Then
                Dim dr As DataRow = dt.Rows(0)
                strOutput += "<div class=""product-column-right-location""><a href=""/contact-us.aspx"" alt=""Global PC Homebase contact information"" Title=""Global PC Homebase contact information"" >Homebase - Shirley</a></div>" & _
                "<div class=""product-column-right-stock"">" & dr("TotalInStock") & "</div>" & _
                "<div class=""product-column-right-committed"">" & dr("TotalCommited") & "</div>" & _
                "<div class=""product-column-right-on-order"">" & dr("TotalOnOrder") & "</div>" & _
                "<div class=""product-column-right-available"">" & (dr("TotalInStock") - dr("TotalCommited")) & "</div>"
            End If
        End If

        dt = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("dbConn"), "xw_GetStockLevelComplete", strProductCode, 10).Tables(0)
        If Not dt Is Nothing Then
            If dt.Rows.Count > 0 Then
                Dim dr As DataRow = dt.Rows(0)
                strOutput += "<div class=""product-column-right-location"">In-Transit</div>" & _
                "<div class=""product-column-right-stock"">" & dr("TotalInStock") & "</div>" & _
                "<div class=""product-column-right-committed"">" & dr("TotalCommited") & "</div>" & _
                "<div class=""product-column-right-on-order"">" & dr("TotalOnOrder") & "</div>" & _
                "<div class=""product-column-right-available"">" & (dr("TotalInStock") - dr("TotalCommited")) & "</div>"
            End If
        End If

        dt = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("dbConn"), "xw_GetWarehouseStockLevelComplete", strProductCode, 11).Tables(0)
        If Not dt Is Nothing Then
            If dt.Rows.Count > 0 Then
                Dim dr As DataRow = dt.Rows(0)
                strOutput += "<div class=""product-column-right-location"">Warehouse</div>" & _
                "<div class=""product-column-right-stock"">" & dr("TotalInStock") & "</div>" & _
                "<div class=""product-column-right-committed"">" & dr("TotalCommited") & "</div>" & _
                "<div class=""product-column-right-on-order"">" & dr("TotalOnOrder") & "</div>" & _
                "<div class=""product-column-right-available"">" & (dr("TotalInStock") - dr("TotalCommited")) & "</div>"
            End If
        End If
        Return strOutput

SQL查询#1:

/****** Object:  StoredProcedure [dbo].[xw_GetWarehouseStockLevelComplete]    Script Date: 05/15/2015 14:32:24 ******/
    SET ANSI_NULLS OFF
    GO
    SET QUOTED_IDENTIFIER OFF
    GO
    ALTER PROCEDURE [dbo].[xw_GetWarehouseStockLevelComplete]
        @ProductID nvarchar(128),
        @Location int
    AS

        DECLARE @TotalInStock int

        SELECT  @TotalInStock = SUM(QTY)  
        FROM    STOCK_LOC_INFO 
        WHERE   Location >= 13 
            AND STOCKCODE=@ProductID

        DECLARE @TotalCommited int

        SELECT  @TotalCommited = SUM(Unsup_quant)  
        FROM    SALESORD_LINES 
        WHERE   Hdr_Status <> 2 
            AND Location >= 13 
            AND STOCKCODE=@ProductID

        DECLARE @TotalOnOrder int

        SELECT  @TotalOnOrder = SUM(BKORD_QUANT)  
        FROM    PURCHORD_LINES 
        WHERE   Location >= 13 
            AND STOCKCODE=@ProductID


        SELECT  isnull(@TotalInStock,0)  as TotalInStock, 
                isnull(@TotalCommited,0) as TotalCommited, 
                isnull(@TotalOnOrder,0)  as TotalOnOrder

SQL查询#2:

/****** Object:  StoredProcedure [dbo].[xw_GetStockLevelComplete]    Script Date: 05/15/2015 14:37:19 ******/
    SET ANSI_NULLS OFF
    GO
    SET QUOTED_IDENTIFIER OFF
    GO

    ALTER PROCEDURE [dbo].[xw_GetStockLevelComplete]
        @ProductID nvarchar(128),
        @Location int
    AS

        DECLARE @TotalInStock int

        SELECT  @TotalInStock = SUM(QTY)  
        FROM    STOCK_LOC_INFO 
        WHERE   Location = @Location 
            AND STOCKCODE=@ProductID

        DECLARE @TotalCommited int

        SELECT  @TotalCommited = SUM(Unsup_quant)  
        FROM    SALESORD_LINES 
        WHERE   Hdr_Status <> 2 
            and Hdr_Status <> 3
            AND HDR_STATUS <> 6  
            AND Location = @Location 
            AND STOCKCODE=@ProductID

        DECLARE @TotalOnOrder int

        SELECT  @TotalOnOrder = SUM(BKORD_QUANT)  
        FROM    PURCHORD_LINES 
        WHERE   Location = @Location 
            AND STOCKCODE=@ProductID


        SELECT  isnull(@TotalInStock,0)     as TotalInStock, 
                isnull(@TotalCommited,0)    as TotalCommited, 
                isnull(@TotalOnOrder,0)     as TotalOnOrder

这是我的新代码:

Public Shared Function GetGalleryStockCheck(ByVal strProductCode As String) As Integer

            Dim intone As Integer = 0
            Dim inttwo As Integer = 0
            'Dim intthree As Integer = 0
            Dim dt1 As DataTable = New DataTable

            dt1 = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("dbConn"), "xw_GetAvailableStockLevel", strProductCode).Tables(0)
            If dt1.Rows.Count > 0 Then
                Dim dr1 As DataRow = dt1.Rows(0)
                intone = (dr1("TotalInStock") - dr1("TotalCommited"))
            End If
            dt1 = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("dbConn"), "xw_GetWarehouseStockLevelComplete", strProductCode, 11).Tables(0)
            If dt1.Rows.Count > 0 Then
                Dim dr1 As DataRow = dt1.Rows(0)
                inttwo = (dr1("TotalInStock") - dr1("TotalCommited"))
            End If
            'dt1 = SqlHelper.ExecuteDataset(System.Configuration.ConfigurationManager.AppSettings("dbConn"), "xw_GetStockLevelComplete", strProductCode, 12).Tables(0)
            'If dt1.Rows.Count > 0 Then
            '    Dim dr1 As DataRow = dt1.Rows(0)
            '    intthree = (dr1("TotalInStock") - dr1("TotalCommited"))
            'End If

            Dim intAvailable = intone + inttwo

            Return intAvailable

        End Function

SQL

/****** Object:  StoredProcedure [dbo].[xw_GetAvailableStockLevel]    Script Date: 05/15/2015 14:33:19 ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER OFF
    GO

    ALTER PROCEDURE [dbo].[xw_GetAvailableStockLevel] 
        @ProductID nvarchar(128)

    AS

        DECLARE @TotalInStock int

        SELECT  @TotalInStock = SUM(QTY)  
        FROM    STOCK_LOC_INFO 
        WHERE   Location IN('4','12') 
            AND STOCKCODE=@ProductID

        DECLARE @TotalCommited int

        SELECT  @TotalCommited = SUM(Unsup_quant)  
        FROM    SALESORD_LINES 
        WHERE   Hdr_Status <> 2 
            and Hdr_Status <> 3 
            AND HDR_STATUS <> 6  
            AND Location IN('4','12') 
            AND STOCKCODE=@ProductID

        DECLARE @WarehouseCommit int

        SELECT  isnull(@TotalInStock,0) as TotalInStock, 
                isnull(@TotalCommited,0) as TotalCommited

1 个答案:

答案 0 :(得分:0)

正如其他人所评论的那样,很难解决这些类型的问题而无法查看所有相关信息。我强烈怀疑(明显的原因,如硬件问题/限制,索引碎片等),问题可能与以下两个问题有关:

  1. 您的查询不可搜索。请参阅here以获得一个很好的解释,但基本上很多谓词中都有函数等,这些函数使它们成为不可分辨的例如DATADED(MONTH,DUEDATE,GETDATE())
  2. 参数嗅探。见here。基本上,当您将参数传递给存储过程时,它们会针对该特定参数进行编译和优化,然后可能会对任何其他不同的参数进行可怕的执行。