1

我正在处理一个非常复杂的查询,其中一部分必须检索与产品关联的类别。类别以递归方式存储在Category表中。产品-类别映射在ProductCategory表格中(从技术上讲,一个产品可以有多个类别,但现在让我们将其从表格中排除,除非它是一个需要考虑的简单变量)。

Category非常简单。一列是CategoryID,另一列是 ,ParentCategoryID第三列是一Name列。由此,类别是嵌套的。表ProductCategory也​​很简单。一列是ProductID另一列CategoryID

我需要检索任何给定产品的顶级和第二大类别。然后我在报告中使用这些信息并进行一些分析。我的解决方案真的很慢并且不能很好地扩展。我不知道如何更有效地提取我需要的数据。

我的解决方案试图做的是将作为特定产品分配类别的父类的所有类别收集在一起,然后抓住我找到的最后两个并将它们返回。我把它作为一个标量函数来完成,我将当前CategoryID和我想要的级别发送回去,所以一个调用为 0,另一个调用为 1。

我的示例代码:

WITH Categories AS (
    SELECT DISTINCT
        CategoryID
    FROM
        ProductCategory
), CategoriesAtDepth AS (
    SELECT
        Categories.CategoryID
        , dbo.WR_f_GetCategoryIDAtDepth(Categories.CategoryID, 0) AS TopCategory
        , dbo.WR_f_GetCategoryIDAtDepth(Categories.CategoryID, 1) AS SecondCategory
    FROM
        Categories
)
SELECT
    CategoriesAtDepth.CategoryID
    , c1.Name AS TopCategory
    , c2.Name AS SecondCategory
FROM
    CategoriesAtDepth LEFT JOIN
    Category AS c1 ON CategoriesAtDepth.TopCategory = c1.CategoryID LEFT JOIN
    Category AS c2 ON CategoriesAtDepth.SecondCategory = c2.CategoryID

以及功能代码:

CREATE FUNCTION WR_f_GetCategoryIDAtDepth 
(
    @CategoryID AS int
    ,@Depth AS int = 0
)
RETURNS int
AS
BEGIN

    -- Declare the return variable here
    DECLARE @Result int
    DECLARE @CurrentHeight int = 0
    DECLARE @CurrentCategoryID int = @CategoryID
    DECLARE @CategoryLevels table
    (
        Height int
        ,CategoryID int
    )

    BEGIN
        --Populate a table with all the categoy IDs in the chain
        WHILE @CurrentCategoryID > 0
        BEGIN
            INSERT INTO @CategoryLevels (Height, CategoryID) VALUES (@CurrentHeight + 1, @CurrentCategoryID)
            SET @CurrentCategoryID = (SELECT ParentCategoryID FROM Category WHERE CategoryID = ISNULL((SELECT CategoryID FROM @CategoryLevels WHERE Height = @CurrentHeight + 1), 0))
            SET @CurrentHeight = @CurrentHeight + 1
        END
        SET @Result = (SELECT CategoryID FROM @CategoryLevels WHERE Height = (@CurrentHeight - @Depth))
    END

    -- Return the result of the function
    RETURN @Result

END
GO

我更多地考虑了@George Mavritsakis 关于使用递归 CTE 的评论,并决定尝试在函数中实现它并提出了这个更快的解决方案:

CREATE FUNCTION WR_f_GetCategoryIDAtDepth 
(
    @CategoryID AS int
    ,@Depth AS int = 0
)
RETURNS int
AS
BEGIN

    -- Declare the return variable here
    DECLARE @Result int
    DECLARE @CategoryLevels table
    (
        Height int
        ,CategoryID int
    )

    BEGIN
        --Populate a table with all the categoy IDs in the chain
        WITH Base AS (
            SELECT
                0 AS Height
                , @CategoryID AS CategoryID

            UNION ALL

            SELECT
                Height + 1
                , ParentCategoryID
            FROM
                Category INNER JOIN
                    Base ON Category.CategoryID = Base.CategoryID
        )
        INSERT INTO @CategoryLevels (Height, CategoryID)
            SELECT * FROM Base

        SET @Result = (SELECT CategoryID FROM @CategoryLevels WHERE Height = ((SELECT MAX(Height) FROM @CategoryLevels) - @Depth - 1))
    END

    -- Return the result of the function
    RETURN @Result

END
GO
4

1 回答 1

1

您必须查看递归 CTE:http ://technet.microsoft.com/en-us/library/ms186243%28v=sql.105%29.aspx

您的解决方案很慢,因为您使用函数WR_f_GetCategoryIDAtDepth多次查询Category表。

于 2013-10-09T14:07:11.567 回答