【问题标题】:Get count of Joins used in stored procedure获取存储过程中使用的连接数
【发布时间】:2016-07-01 10:04:13
【问题描述】:
CREATE PROCEDURE [dbo].[uspGetBillOfMaterials]
@StartProductID [int],
@CheckDate [datetime] AS BEGIN

WITH [BOM_cte]([ProductAssemblyID], [ComponentID], [ComponentDesc], [PerAssemblyQty], [StandardCost], [ListPrice], [BOMLevel], [RecursionLevel]) -- CTE name and columns
AS (
    SELECT b.[ProductAssemblyID], b.[ComponentID], p.[Name], b.[PerAssemblyQty], p.[StandardCost], p.[ListPrice], b.[BOMLevel], 0 -- Get the initial list of components for the bike assembly
    FROM [Production].[BillOfMaterials] b
        INNER JOIN [Production].[Product] p 
        ON b.[ComponentID] = p.[ProductID] 
    WHERE b.[ProductAssemblyID] = @StartProductID 
        AND @CheckDate >= b.[StartDate] 
        AND @CheckDate <= ISNULL(b.[EndDate], @CheckDate)
    UNION ALL
    SELECT b.[ProductAssemblyID], b.[ComponentID], p.[Name], b.[PerAssemblyQty], p.[StandardCost], p.[ListPrice], b.[BOMLevel], [RecursionLevel] + 1 -- Join recursive member to anchor
    FROM [BOM_cte] cte
        INNER JOIN [Production].[BillOfMaterials] b 
        ON b.[ProductAssemblyID] = cte.[ComponentID]
        INNER JOIN [Production].[Product] p 
        ON b.[ComponentID] = p.[ProductID] 
    WHERE @CheckDate >= b.[StartDate] 
        AND @CheckDate <= ISNULL(b.[EndDate], @CheckDate)
    )
-- Outer select from the CTE
SELECT b.[ProductAssemblyID], b.[ComponentID], b.[ComponentDesc], SUM(b.[PerAssemblyQty]) AS [TotalQuantity] , b.[StandardCost], b.[ListPrice], b.[BOMLevel], b.[RecursionLevel]
FROM [BOM_cte] b
GROUP BY b.[ComponentID], b.[ComponentDesc], b.[ProductAssemblyID], b.[BOMLevel], b.[RecursionLevel], b.[StandardCost], b.[ListPrice]
ORDER BY b.[BOMLevel], b.[ProductAssemblyID], b.[ComponentID] END;

我想获取在存储过程的查询批次中使用的连接数。我需要验证批量连接计数不超过 4。

【问题讨论】:

  • 您是按程序查找four joins,还是按程序中的每个语句查找four joins
  • @gotqn ,我正在寻找过程中每个语句的四个连接

标签: sql-server join stored-procedures adventureworks


【解决方案1】:

这样试试,

DECLARE @String NVARCHAR(4000) = 
    'CREATE PROCEDURE [dbo].[uspGetBillOfMaterials]
@StartProductID [int],
@CheckDate [datetime] AS BEGIN

WITH [BOM_cte]([ProductAssemblyID], [ComponentID], [ComponentDesc], [PerAssemblyQty], [StandardCost], [ListPrice], [BOMLevel], [RecursionLevel]) -- CTE name and columns
AS (
    SELECT b.[ProductAssemblyID], b.[ComponentID], p.[Name], b.[PerAssemblyQty], p.[StandardCost], p.[ListPrice], b.[BOMLevel], 0 -- Get the initial list of components for the bike assembly
    FROM [Production].[BillOfMaterials] b
        INNER JOIN [Production].[Product] p 
        ON b.[ComponentID] = p.[ProductID] 
    WHERE b.[ProductAssemblyID] = @StartProductID 
        AND @CheckDate >= b.[StartDate] 
        AND @CheckDate <= ISNULL(b.[EndDate], @CheckDate)
    UNION ALL
    SELECT b.[ProductAssemblyID], b.[ComponentID], p.[Name], b.[PerAssemblyQty], p.[StandardCost], p.[ListPrice], b.[BOMLevel], [RecursionLevel] + 1 -- Join recursive member to anchor
    FROM [BOM_cte] cte
        INNER JOIN [Production].[BillOfMaterials] b 
        ON b.[ProductAssemblyID] = cte.[ComponentID]
        INNER JOIN [Production].[Product] p 
        ON b.[ComponentID] = p.[ProductID] 
    WHERE @CheckDate >= b.[StartDate] 
        AND @CheckDate <= ISNULL(b.[EndDate], @CheckDate)
    )
-- Outer select from the CTE
SELECT b.[ProductAssemblyID], b.[ComponentID], b.[ComponentDesc], SUM(b.[PerAssemblyQty]) AS [TotalQuantity] , b.[StandardCost], b.[ListPrice], b.[BOMLevel], b.[RecursionLevel]
FROM [BOM_cte] b
GROUP BY b.[ComponentID], b.[ComponentDesc], b.[ProductAssemblyID], b.[BOMLevel], b.[RecursionLevel], b.[StandardCost], b.[ListPrice]
ORDER BY b.[BOMLevel], b.[ProductAssemblyID], b.[ComponentID] END;'
    ,@SubString VARCHAR(255) = 'JOIN'

SELECT (LEN(@String)-LEN(REPLACE(@String, @SubString, '')))/LEN(@SubString) as count

【讨论】:

  • 谢谢!现在我可以得到完整存储过程的加入计数。仍然有一种方法可以在存储过程的单个查询中找到连接计数。我的意思是存储过程中的查询可以相互分离,而不是被视为一个批次..
猜你喜欢
  • 1970-01-01
  • 2018-07-25
  • 1970-01-01
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 2015-11-10
  • 2016-08-14
  • 2017-01-01
相关资源
最近更新 更多