SQL递归层次结构非常慢

时间:2016-01-11 13:05:37

标签: c# sql sql-server recursion hierarchy

所以我刚刚完成了一项优化SQL查询的任务,该查询构建了产品,内部产品,工厂操作,操作细节和项目(制作产品所需)的层次结构树。查询返回218k行,正常工作但构建树需要大约15-18秒。是否可以优化该查询,因此大约需要2-3秒?如果我将@ProductID添加为此过程的参数并执行

WHERE ProductID = @ProductID

从Product表中选择后,它的工作速度非常快,但是树不完整,因为如果我构建树的产品是内部产品,那么它的父产品不会显示在树中。 将所有这些表加载到C#应用程序并在那里构建所有层次结构会更快吗?

我对所有想法持开放态度。以下是有问题的查询

  ALTER PROCEDURE [dbo].[ProductProduct_selectHierarchy_v2]
WITH EXECUTE AS CALLER
AS
BEGIN
WITH ProductProductHierarchy (
    ProductID
    ,ItemProductID
    ,LEVEL
    ,UniqueID
    ,ParentID
    ,RowType
    ,ProductDetailID
    ,ProductProductDetailBindID
    ,ManufactoryOperationID
    ,ItemID
    )
AS (
    -- Anchor member definition
    SELECT dbo.Product.ProductID AS ProductID
        ,Product.ProductID AS ItemProductID
        ,0 AS LEVEL
        ,Cast(cast(Product.ProductID AS NVARCHAR(40)) + '' AS NVARCHAR(50)) AS UniqueID
        ,cast('' AS NVARCHAR(50)) AS ParentID
        ,1 AS RowType
        ,NULL AS ProductDetailID
        ,NULL AS ProductProductDetailBindID
        ,NULL AS ManufactoryOperationID
        ,NULL AS ItemID
    FROM dbo.Product

    UNION ALL

    -- Recursive member definition
    SELECT e.ProductID
        ,e.ItemProductID
        ,LEVEL + 1
        ,Cast(Cast(d.UniqueID AS NVARCHAR(40)) + '_' + cast((e.ItemProductID) AS NVARCHAR(10)) AS NVARCHAR(50)) AS UniqueID
        ,Cast(d.UniqueID AS NVARCHAR(50)) AS ParentID
        ,1 AS RowType
        ,NULL AS ProductDetailID
        ,NULL AS ProductProductDetailBindID
        ,NULL AS ManufactoryOperationID
        ,NULL AS ItemID
    FROM dbo.ProductProduct AS e
    INNER JOIN ProductProductHierarchy AS d ON e.ProductID = d.ItemProductID

    UNION ALL

    SELECT NULL AS ProductID
        ,NULL
        ,LEVEL + 1
        ,Cast(Cast(d.UniqueID AS NVARCHAR(40)) + '_' + cast((e.ProductDetailID) AS NVARCHAR(10)) AS NVARCHAR(50)) AS UniqueID
        ,Cast(d.UniqueID AS NVARCHAR(50)) AS ParentID
        ,2 AS RowType
        ,e.ProductDetailID AS ProductDetailID
        ,e.ProductProductDetailBindID AS ProductProductDetailBindID
        ,NULL AS ManufactoryOperationID
        ,NULL AS ItemID
    FROM dbo.ProductProductDetailBind AS e
    INNER JOIN ProductProductHierarchy AS d ON e.ProductID = d.ItemProductID

    UNION ALL

    SELECT NULL AS ProductID
        ,NULL
        ,LEVEL + 1
        ,Cast(Cast(d.UniqueID AS NVARCHAR(40)) + '_' + cast((e.ManufactoryOperationID) AS NVARCHAR(10)) AS NVARCHAR(50)) AS UniqueID
        ,Cast(d.UniqueID AS NVARCHAR(50)) AS ParentID
        ,3 AS RowType
        ,NULL AS ProductDetailID
        ,NULL AS ProductProductDetailBindID
        ,e.ManufactoryOperationID AS ManufactoryOperationID
        ,NULL AS ItemID
    FROM dbo.ProductDetailOperation AS e
    INNER JOIN ProductProductHierarchy AS d ON e.ProductProductDetailBindID = d.ProductProductDetailBindID

    UNION ALL

    SELECT NULL AS ProductID
        ,NULL
        ,LEVEL + 1
        ,Cast(Cast(d.UniqueID AS NVARCHAR(40)) + '_' + cast((e.ItemID) AS NVARCHAR(10)) AS NVARCHAR(50)) AS UniqueID
        ,Cast(d.UniqueID AS NVARCHAR(50)) AS ParentID
        ,4 AS RowType
        ,NULL AS ProductDetailID
        ,NULL AS ProductProductDetailBindID
        ,NULL AS ManufactoryOperationID
        ,e.ItemID AS ItemID
    FROM dbo.ProductItem AS e
    INNER JOIN ProductProductHierarchy AS d ON e.ProductID = d.ItemProductID
    )
-- Statement that executes the CTE
SELECT CASE 
        WHEN RowType = 1
            THEN ProductName + '<' + ProductCode + '>'
        WHEN RowType = 2
            THEN ProductDetail.ProductDetailName
        WHEN RowType = 3
            THEN ManufactoryOperation.ManufactoryOperationName
        ELSE Item.ItemName
        END AS ProductName
    ,UniqueID
    ,ProductProductHierarchy.ParentID
    ,Product.ProductID
    ,RowType
FROM ProductProductHierarchy
LEFT OUTER JOIN Product ON Product.ProductID = ProductProductHierarchy.ItemProductID
    AND RowType = 1
LEFT OUTER JOIN ProductDetail ON ProductDetail.ProductDetailID = ProductProductHierarchy.ProductDetailID
    AND RowType = 2
LEFT OUTER JOIN ManufactoryOperation ON ManufactoryOperation.ManufactoryOperationID = ProductProductHierarchy.ManufactoryOperationID
    AND RowType = 3
LEFT OUTER JOIN Item ON Item.ItemID = ProductProductHierarchy.ItemID
    AND RowType = 4
OPTION (MAXRECURSION 0)

END

1 个答案:

答案 0 :(得分:0)

看起来你正在有效地构建一组异构行,我会试着远离这些行。如果需要,使用递归获取产品树,然后将操作,项目等作为单独的结果集引入。混合行类型很可能会导致头痛(不仅仅是性能问题)。

在不知道应用程序的全部功能的情况下,我不能说你应该从数据库中检索这些项目的最佳方式,但我肯定会从那里开始作为一个可能的问题。