TSQL CTE递归查询的优化

时间:2017-09-12 16:38:04

标签: sql-server performance tsql recursion ssms

我有以下CTE使用递归来获取Hierarhcy中的每个节点的级别,最后,因为我有27个级别,所以我抓住每个级别的名称,因为最终用户不想看到的GUID。

    with EmpTree

    as
    (
      select e.DWH_Dim_TFS_File_DWH_File_Guid, cast(cast(e.DWH_Dim_TFS_File_DWH_File_Guid as binary(4)) as varbinary(max)) as EmpHier,
      1 as EmployeeLevel

      from [DWH].[Dim_TFS_File_View] as e
      where e.DWH_Dim_TFS_File_DWH_FileParent_Guid is null 
      union all
      select c.DWH_Dim_TFS_File_DWH_File_Guid, cast(p.EmpHier + cast(c.DWH_Dim_TFS_File_DWH_File_Guid as binary(4)) as varbinary(max)),
      EmployeeLevel +1 as EmployeeLevel
      from EmpTree as p
      join [DWH].[Dim_TFS_File_View] as c
        on c.DWH_Dim_TFS_File_DWH_FileParent_Guid = p.DWH_Dim_TFS_File_DWH_File_Guid
    )

    select TOP 100 PERCENT DWH_Dim_TFS_File_DWH_File_Guid
         ,EmployeeLevel
         ,(SELECT [File_Name] from [DWH].[Dim_TFS_File_View] as pu where nullif(cast(substring(EmpHier, 1, 4) as int), 0) = pu.DWH_Dim_TFS_File_DWH_File_Guid) level1
         ,(SELECT [File_Name] from [DWH].[Dim_TFS_File_View] as pu where nullif(cast(substring(EmpHier, 1, 9) as int), 0) = pu.DWH_Dim_TFS_File_DWH_File_Guid) level2
         ,(SELECT [File_Name] from [DWH].[Dim_TFS_File_View] as pu where nullif(cast(substring(EmpHier, 1, 13) as int), 0) = pu.DWH_Dim_TFS_File_DWH_File_Guid) level3
from EmpTree
order by DWH_Dim_TFS_File_DWH_File_Guid

我有27个等级...... 我抓取信息的视图有三个索引: (父母,子女) (父) (儿童)

这个表有200M行并且还在增长..所以这个查询非常慢,我想大部分也会导致每个级别的所有“名字抓取”......

这是一种更有效的方式来实现我的结果吗?也许通过一些加入?

如果可以,请帮忙

谢谢!

没有获得正确的递归,只获取Anchor部分。

with EmpTree

as
(
  select e.DWH_Dim_TFS_File_DWH_FileParent_Guid,e.DWH_Dim_TFS_File_DWH_File_Guid,
  1 as Depth,
  File_Name_String = CAST(CAST(e.File_Name AS BINARY(100)) AS VARBINARY(8000))

  from [dbo].[Hierarchy_Luis] as e
  where e.DWH_Dim_TFS_File_DWH_FileParent_Guid is null 
  union all
  select e.DWH_Dim_TFS_File_DWH_FileParent_Guid,e.DWH_Dim_TFS_File_DWH_File_Guid,
  p.Depth +1 as Depth,
  File_Name_String = CAST(CONCAT(p.File_Name_String, CAST(e.File_Name AS BINARY(100))) AS VARBINARY(8000))
  from [dbo].[Hierarchy_Luis] as e 
  join EmpTree as p
    on e.DWH_Dim_TFS_File_DWH_FileParent_Guid = p.DWH_Dim_TFS_File_DWH_File_Guid
)

SELECT
        p.DWH_Dim_TFS_File_DWH_File_Guid,
        p.Depth,
      Level01 =CAST(SUBSTRING(p.File_Name_String,   1, 100) as nvarchar(100)),
      Level02 =CAST(SUBSTRING(p.File_Name_String,   101, 100) as nvarchar(100)),
      Level03 =CAST(SUBSTRING(p.File_Name_String,   201, 100) as nvarchar(100)),
      Level04 =CAST(SUBSTRING(p.File_Name_String,   301, 100) as nvarchar(100)),
      Level05 =CAST(SUBSTRING(p.File_Name_String,   401, 100) as nvarchar(100)),
      Level07 =CAST(SUBSTRING(p.File_Name_String,   501, 100) as nvarchar(100)),
      Level08 =CAST(SUBSTRING(p.File_Name_String,   601, 100) as nvarchar(100)),
      Level09 =CAST(SUBSTRING(p.File_Name_String,   701, 100) as nvarchar(100)),
      Level10 =CAST(SUBSTRING(p.File_Name_String,   801, 100) as nvarchar(100)),
      Level11 =CAST(SUBSTRING(p.File_Name_String,   901, 100) as nvarchar(100)),
      Level12 =CAST(SUBSTRING(p.File_Name_String,   1001, 100) as nvarchar(100)),
      Level13 =CAST(SUBSTRING(p.File_Name_String,   1101, 100) as nvarchar(100)),
      Level14 =CAST(SUBSTRING(p.File_Name_String,   1201, 100) as nvarchar(100)),
      Level15 =CAST(SUBSTRING(p.File_Name_String,   1301, 100) as nvarchar(100)),
      Level16 =CAST(SUBSTRING(p.File_Name_String,   1401, 100) as nvarchar(100)),
      Level17 =CAST(SUBSTRING(p.File_Name_String,   1501, 100) as nvarchar(100)),
      Level18 =CAST(SUBSTRING(p.File_Name_String,   1601, 100) as nvarchar(100)),
      Level19 =CAST(SUBSTRING(p.File_Name_String,   1701, 100) as nvarchar(100)),
      Level20 =CAST(SUBSTRING(p.File_Name_String,   1801, 100) as nvarchar(100)),
      Level21 =CAST(SUBSTRING(p.File_Name_String,   1901, 100) as nvarchar(100)),
      Level22 =CAST(SUBSTRING(p.File_Name_String,   2001, 100) as nvarchar(100)),
      Level23 =CAST(SUBSTRING(p.File_Name_String,   2101, 100) as nvarchar(100)),
      Level24 =CAST(SUBSTRING(p.File_Name_String,   2201, 100) as nvarchar(100)),
      Level25 =CAST(SUBSTRING(p.File_Name_String,   2301, 100) as nvarchar(100)),
      Level26 =CAST(SUBSTRING(p.File_Name_String,   2401, 100) as nvarchar(100)),
      Level27 =CAST(SUBSTRING(p.File_Name_String,   2501, 100) as nvarchar(100))

    FROM EmpTree p

1 个答案:

答案 0 :(得分:1)

如果没有具有代表性的测试数据和预期结果,很难确定细节......以下内容应该在球部分......

IF OBJECT_ID('tempdb..#Dim_TFS_File_View', 'U') IS NOT NULL 
DROP TABLE #Dim_TFS_File_View;
GO 
CREATE TABLE #Dim_TFS_File_View (
    DWH_Dim_TFS_File_DWH_File_Guid INT NOT NULL,
    DWH_Dim_TFS_File_DWH_FileParent_Guid INT NULL,
    [File_Name] UNIQUEIDENTIFIER NOT NULL 
    );
INSERT #Dim_TFS_File_View (DWH_Dim_TFS_File_DWH_File_Guid, DWH_Dim_TFS_File_DWH_FileParent_Guid, [File_Name])
SELECT 
    DWH_Dim_TFS_File_DWH_File_Guid = t.n,
    DWH_Dim_TFS_File_DWH_FileParent_Guid = CASE WHEN t.n = 1 THEN NULL ELSE ISNULL(NULLIF(mi.ManagerID -1, 0), 1) END,
    [File_Name] = NEWID()
FROM 
    dbo.tfn_Tally(1000, 1) t
    CROSS APPLY ( VALUES (ABS(CHECKSUM(NEWID())) % t.n + 1) ) mi (ManagerID);

-- SELECT * FROM #Dim_TFS_File_View dtfv;

WITH 
    cte_Recursion AS (
        SELECT 
            e.DWH_Dim_TFS_File_DWH_File_Guid,
            NodeLevel = 1,
            File_Name_String = CAST(CAST(e.[File_Name] AS BINARY(16)) AS VARBINARY(8000))
        FROM 
            #Dim_TFS_File_View e
        WHERE 
            e.DWH_Dim_TFS_File_DWH_FileParent_Guid IS NULL
        UNION ALL 
        SELECT 
            e.DWH_Dim_TFS_File_DWH_File_Guid,
            NodeLevel = r.NodeLevel + 1,
            File_Name_String = CAST(CONCAT(r.File_Name_String, CAST(e.[File_Name] AS BINARY(16))) AS VARBINARY(8000))
        FROM 
            cte_Recursion r
            JOIN #Dim_TFS_File_View e
                ON r.DWH_Dim_TFS_File_DWH_File_Guid = e.DWH_Dim_TFS_File_DWH_FileParent_Guid
        )
SELECT
    r.DWH_Dim_TFS_File_DWH_File_Guid,
    r.NodeLevel,
    Level01 = NULLIF(CAST(SUBSTRING(r.File_Name_String,   1, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level02 = NULLIF(CAST(SUBSTRING(r.File_Name_String,  17, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level03 = NULLIF(CAST(SUBSTRING(r.File_Name_String,  33, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level04 = NULLIF(CAST(SUBSTRING(r.File_Name_String,  49, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level05 = NULLIF(CAST(SUBSTRING(r.File_Name_String,  65, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level06 = NULLIF(CAST(SUBSTRING(r.File_Name_String,  81, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level07 = NULLIF(CAST(SUBSTRING(r.File_Name_String,  97, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level08 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 113, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level09 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 129, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level10 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 145, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level11 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 161, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level12 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 177, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level13 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 193, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level14 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 209, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level15 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 225, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level16 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 241, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level17 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 257, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level18 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 273, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level19 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 289, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level20 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 305, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level21 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 321, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level22 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 337, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level23 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 353, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level24 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 369, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level25 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 385, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level26 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 401, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000'),
    Level27 = NULLIF(CAST(SUBSTRING(r.File_Name_String, 417, 16) AS UNIQUEIDENTIFIER), '00000000-0000-0000-0000-000000000000')
FROM
    cte_Recursion r;

输出样本......

DWH_Dim_TFS_File_DWH_File_Guid NodeLevel   Level01                              Level02                              Level03                              Level04                              Level05                              Level06                              Level07                              Level08                              Level09                              Level10                              Level11                              Level12                              Level13                              Level14                              Level15                              Level16                              Level17                              Level18                              Level19                              Level20                              Level21                              Level22                              Level23                              Level24                              Level25                              Level26                              Level27
------------------------------ ----------- ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------ ------------------------------------
1                              1           F7CCFA67-F558-41EE-B1BF-8D96692AB99F NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL
2                              2           F7CCFA67-F558-41EE-B1BF-8D96692AB99F 34CAF6D3-44BA-4DE1-9E03-836EE38C43E3 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL
3                              2           F7CCFA67-F558-41EE-B1BF-8D96692AB99F A69D9BE6-D83A-40AB-9D5B-04219317A3F9 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL                                 NULL