T-SQL的CTE连接性能不佳

时间:2018-06-22 10:22:03

标签: sql sql-server common-table-expression

长话短说。我有两个非常相似的SQL查询,并输出短名称和长名称的值对。输入参数用于第一个查询:短名称,第二个查询是长名称。查询被构造为输出包含输入参数的所有行,但并不完全匹配(例如,如果我将标记名设置为ST2,则此查询将输出所有包含ST2的对象以及所有其他名称开头为ST2的对象。 / p>

所有查询都在同一数据库中执行。在下面的查询中,输入参数设置为"",这意味着查询输出所有对象的短名称和长名称的键值对

declare @tagName as varchar(50) = ''
set @tagName = @tagName + '.'

-- this one query outputs ~700 rows
;with AnalogTag as
(
Select *
from [Runtime].[dbo].[AnalogTag] 
where (substring(TagName, 0, charindex('.',@tagName))) in (substring(@tagName,0, len(@tagName))) 
and (substring(TagName, charindex('.',TagName), 2)) not in ('.#')
),

-- this one query outputs ~7000 rows
HierarchicalName as
(
Select * 
from [proba7].[dbo].[internal_list_objects_view]
where substring(tag_name, 0,len(@tagName)) = substring(@tagName,0, len(@tagName))
)
select HierarchicalName.tag_name as TagName
,HierarchicalName.hierarchical_name ilo_view_HierarchicalName


from AnalogTag
inner join HierarchicalName
on substring(AnalogTag.TagName, 0, CHARINDEX('.',AnalogTag.TagName)) = HierarchicalName.tag_name

上面的整个查询运行大约3秒钟。并输出约450行

我在同一数据库上创建了一个类似的查询:

declare @hierarchicalName as varchar(200) = ''
declare @Length as int
set @Length = LEN(@hierarchicalName)+1

 -- this query outputs approx 700 rows and if runs separately it runs 
 --almost instantly
;with AnalogTag as
(
Select TagName 
from [Runtime].[dbo].[AnalogTag] 
where (substring(TagName, 0, CHARINDEX('.',TagName))) in 
    (
    Select tag_name from [proba7].[dbo].[internal_list_objects_view]
    where substring(hierarchical_name, 0, @Length) = @hierarchicalName
    )
and (substring(TagName, CHARINDEX('.',TagName), 2)) not in ('.#')
),
 -- this query outputs approx 7000 rows and if runs separately it runs 
 --almost instantly
HierarchicalName as
(
    Select hierarchical_name, tag_name from [proba7].[dbo].[internal_list_objects_view]
    where substring(hierarchical_name, 0, @Length) = @hierarchicalName
)
select HierarchicalName.tag_name as ilo_view_TagName
,HierarchicalName.hierarchical_name ilo_view_HierarchicalName

from AnalogTag
inner join HierarchicalName
on substring(AnalogTag.TagName, 0, CHARINDEX('.',AnalogTag.TagName)) = HierarchicalName.tag_name

这次查询将在28秒内运行。输出与第一个查询相似的行数(因为这两个查询的ouptut必须相似)。我注意到,如果我将“内部联接”更改为“完全联接”,查询将立即运行。

模拟标记示例输出:

 TagName                               a    b   c   d   e   f   g   h   j   j   k   l   m
    PomFPTemp.PV                     1062   0   10  0   10  0   4   0   0   0   0   0   0
    PomFPWilgWzgl.PV                   1    0   10  0   10  0   4   0   0   0   0   0   0
    SNST3P1BrakFWHMocD3f.PV          1063   0   10  0   10  0   4   0   0   0   0   0   0
    SNST3P1BrakFWHMocP3f.PV           46    0   10  0   10  0   4   0   0   0   0   0   0
    SNST3P1BrakFWHMocQ3f.PV          1063   0   10  0   10  0   4   0   0   0   0   0   0
    SNST3P1BrakFWHMocQ3fIntExp.PV    1063   0   10  0   10  0   4   0   0   0   0   0   0
    SNST3P1BrakFWHMocQ3fIntImp.PV    1063   0   10  0   10  0   4   0   0   0   0   0   0
    SNST3P1BrakFWHMocQn3f.PV         1063   0   10  0   10  0   4   0   0   0   0   0   0
    SNST3P1EnkvarhExp3f.PV           1060   0   10  0   10  0   4   0   0   0   0   0   0
    SNST3P1EnkvarhImp3f.PV           1060   0   10  0   10  0   4   0   0   0   0   0   0

分层名称示例输出:

ID tag_name            contained_name               hierarchical_name       a   b   c   d   e   f   g   h   i   j   k   l   m   n   o   p   q   r   s   t   u   v   w   x   y   z   aa  bb  cc  dd  ee  ff  gg  hh
1   $Galaxy                                         $Galaxy                     1   0   0   0   0   1       NULL    NULL    0   0   1   1   1   0   0   0   0   0   1   0   NULL    23  0   0   0   0   0   0   0   0   0   133123  50026   1   NULL
2   proba7                                          Galaxy_001                  0   0   0   0   1   1       NULL    NULL    0   0   848 1   0   1   0   0   0   0   1   0   NULL    23  0   0   0   0   0   0   0   0   0   4098    878020  1   NULL
3   $_AutoImport                                    $_AutoImport               1    0   0   0   0   3       NULL    NULL    0   0   4   2   0   0   0   0   0   0   1   0   NULL    10  0   0   0   0   0   0   0   0   0   131075  3699    1   NULL
4   $_DiCommon                                      $_DiCommon                 1    0   0   0   0   4       NULL    NULL    0   0   5   3   0   0   0   0   0   0   1   0   NULL    10  0   0   0   0   0   0   0   0   0   131075  50023   1   NULL
5   $WinPlatform                                    $WinPlatform               1    0   0   0   0   5       NULL    1   0   0   6   4   1   0   0   0   0   0   0   0   NULL    1   0   0   0   0   0   0   0   0   0   133121  419340  1   1
6   $AppEngine                                      $AppEngine                 1    0   0   0   0   6       NULL    1   0   0   7   5   1   0   0   0   0   0   0   0   NULL    3   0   0   0   0   0   0   0   0   0   133121  419341  1   1
7   $Area                                           $Area                      1    0   0   0   0   7       NULL    1   0   0   8   6   1   0   0   0   0   0   0   0   NULL    13  0   0   0   0   0   0   0   0   0   133121  3452998 1   1
8   $AnalogDevice                                   $AnalogDevice              1    0   0   0   0   8       NULL    2   0   0   9   7   0   0   0   0   0   0   0   0   NULL    10  0   0   0   0   0   0   0   0   0   131073  419343  1   2
9   $DDESuiteLinkClient                          $DDESuiteLinkClient               1    0   0   0   0   9       NULL    3   0   0   10  8   1   0   0   0   0   0   0   0   NULL    11  0   0   0   0   0   0   0   0   0   133121  419344  1   3
10  $DiscreteDevice                              $DiscreteDevice                   1    0   0   0   0   10      NULL    2   0   0   11  9   0   0   0   0   0   0   0   0   NULL    10  0   0   0   0   0   0   0   0   0   131073  419345  1   2
11  $InTouchProxy                                 $InTouchProxy                1    0   0   0   0   11      NULL    3   0   0   12  10  0   0   0   0   0   0   0   0   NULL    11  0   0   0   0   0   0   0   0   0   131073  419346  1   3

输出表(示例):

ilo_view_TagName    ilo_view_HierarchicalName   
ST4FP12Rozl         ST4_FP1.Galaz_2.Rozladowywanie  
ST4FP21Rozl         ST4_FP2.Galaz_1.Rozladowywanie  
ST4FP22Rozl         ST4_FP2.Galaz_2.Rozladowywanie  
ST4FP31Rozl         ST4_FP3.Galaz_1.Rozladowywanie  
ST4RS41AnWspKFL2    ST4_S1_RS4_1.Wsp.K_Factor.L2    
ST4FP32Rozl         ST4_FP3.Galaz_2.Rozladowywanie  
ST4RS31AnWspKFL2    ST4_S2_RS3_1.Wsp.K_Factor.L2    
ST4RS51AnWspKFL2    ST4_S3_RS5_1.Wsp.K_Factor.L2    
ST4FP11U            ST4_FP1.Galaz_1.Napiecie    

最诚挚的问候,并感谢您的任何建议。我尽力使该示例表可读。

0 个答案:

没有答案