如何正确索引sql server表以提高查询性能

时间:2014-02-17 14:42:05

标签: sql-server indexing query-tuning

我需要为临时表添加正确的索引以加快查询速度。根据msdn我检查菜单“包括实际执行计划”并在SSMC中运行查询,执行后我可以看到查询计划,但没有关于要创建的索引的指示。

表@Tax中的字段l1,l2 .... l10标识元素在层次结构中的位置:

样本中的虚拟数据表示以下结构:

--node idleaf=1
----child idleaf=3 of 1
--------child idleaf=5 of 3
----child idleaf=4 of 1
--node idleaf=2

我需要提取巨大的查询,从顶部开始提取每条记录的所有父母的列表 例如:

这是查询的结果(省略某些字段对样本不感兴趣) idleaf介绍NODE_PATH

1    node idleaf=1      1_________
3    child idleaf=3 of 1    1_3________
5    child idleaf=5 of 3    1_3_5_______
4    child idleaf=4 of 1    1_4________
2    node idleaf=2      2_________

查询:

Create Table #Tax (IdLeaf int,l1 int,l2 int ,l3 int ,l4 int ,l5 int ,l6 int 
,l7 int   ,l8     int ,l9 int ,l10 int ,Descrizione varchar(max), FlagDocumento bit)


INSERT INTO #Tax (IdLeaf,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,Descrizione,FlagDocumento)
VALUES (1,1,0,0,0,0,0,0,0,0,0,'node idleaf=1',1)

INSERT INTO #Tax (IdLeaf,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,Descrizione,FlagDocumento)
VALUES (2,2,0,0,0,0,0,0,0,0,0,'node idleaf=2',1)

INSERT INTO #Tax (IdLeaf,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,Descrizione,FlagDocumento)
VALUES (3,1,1,0,0,0,0,0,0,0,0,'child idleaf=3 of 1',1)

INSERT INTO #Tax (IdLeaf,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,Descrizione,FlagDocumento)
VALUES (4,1,2,0,0,0,0,0,0,0,0,'child idleaf=4 of 1',1)

INSERT INTO #Tax (IdLeaf,l1,l2,l3,l4,l5,l6,l7,l8,l9,l10,Descrizione,FlagDocumento)
VALUES (5,1,1,1,0,0,0,0,0,0,0,'child idleaf=5 of 3',1)

SELECT Levels.IdLeaf , Levels.Descrizione,Levels.FlagDocumento,
CASE FlagDocumento WHEN 1 Then
    ISNULL(CONVERT(varchar(10),Levels.Level1),'') + '_' + 
    ISNULL(CONVERT(varchar(10),Levels.Level2),'') + '_' + 
    ISNULL(CONVERT(varchar(10),Levels.Level3),'') + '_' + 
    ISNULL(CONVERT(varchar(10),Levels.Level4),'') + '_' + 
    ISNULL(CONVERT(varchar(10),Levels.Level5),'') + '_' + 
    ISNULL(CONVERT(varchar(10),Levels.Level6),'') + '_' + 
    ISNULL(CONVERT(varchar(10),Levels.Level7),'') + '_' + 
    ISNULL(CONVERT(varchar(10),Levels.Level8),'') + '_' + 
    ISNULL(CONVERT(varchar(10),Levels.Level9),'') + '_' + 
    ISNULL(CONVERT(varchar(10),Levels.Level10),'')  
Else '' END AS NodePath,
CASE 
    When Levels.Level10>0 Then Levels.IdLeaf9
    When Levels.Level9>0 Then Levels.IdLeaf8
    When Levels.Level8>0 Then Levels.IdLeaf7
    When Levels.Level7>0 Then Levels.IdLeaf6
    When Levels.Level6>0 Then Levels.IdLeaf5
    When Levels.Level5>0 Then Levels.IdLeaf4
    When Levels.Level4>0 Then Levels.IdLeaf3
    When Levels.Level3>0 Then Levels.IdLeaf2
    When Levels.Level2>0 Then Levels.IdLeaf1
    When Levels.Level1>0 Then null
END AS IdParent,
CASE 
    When Levels.Level10>0 Then 10
    When Levels.Level9>0 Then 9
    When Levels.Level8>0 Then 8
    When Levels.Level7>0 Then 7
    When Levels.Level6>0 Then 6
    When Levels.Level5>0 Then 5
    When Levels.Level4>0 Then 4
    When Levels.Level3>0 Then 3
    When Levels.Level2>0 Then 2
    When Levels.Level1>0 Then 1

END AS TaxLevel
FROM 
(
   SELECT f10.*, 
   f1.IdLeaf AS IdLeaf1, 
   f2.IdLeaf AS IdLeaf2, 
   f3.IdLeaf AS IdLeaf3, 
   f4.IdLeaf AS IdLeaf4, 
   f5.IdLeaf AS IdLeaf5, 
   f6.IdLeaf AS IdLeaf6, 
   f7.IdLeaf AS IdLeaf7, 
   f8.IdLeaf AS IdLeaf8, 
   f9.IdLeaf AS IdLeaf9, 
   f10.IdLeaf AS IdLeaf10, 
   CONVERT(varchar(10), f1.IdLeaf) + '_' + CONVERT(varchar(10), f2.IdLeaf) + '_' + CONVERT(varchar(10), f3.IdLeaf) + '_' + CONVERT(varchar(10), f4.IdLeaf) +  CONVERT(varchar(10), f5.IdLeaf) + CONVERT(varchar(10), f6.IdLeaf) + CONVERT(varchar(10), f7.IdLeaf) + CONVERT(varchar(10), f8.IdLeaf) + CONVERT(varchar(10), f9.IdLeaf) + CONVERT(varchar(10), f10.IdLeaf) + '_' AS NODE_PATH,
   f1.IdLeaf AS 'Level1',
     CASE WHEN f2.IdLeaf <> f1.IdLeaf THEN f2.IdLeaf END AS 'Level2',
     CASE WHEN f3.IdLeaf <> f2.IdLeaf THEN f3.IdLeaf END AS 'Level3',
     CASE WHEN f4.IdLeaf <> f3.IdLeaf THEN f4.IdLeaf END AS 'Level4',
     CASE WHEN f5.IdLeaf <> f4.IdLeaf THEN f5.IdLeaf END AS 'Level5',
     CASE WHEN f6.IdLeaf <> f5.IdLeaf THEN f6.IdLeaf END AS 'Level6',
     CASE WHEN f7.IdLeaf <> f6.IdLeaf THEN f7.IdLeaf END AS 'Level7',
     CASE WHEN f8.IdLeaf <> f7.IdLeaf THEN f8.IdLeaf END AS 'Level8',
     CASE WHEN f9.IdLeaf <> f8.IdLeaf THEN f9.IdLeaf END AS 'Level9',
     CASE WHEN f10.IdLeaf <> f9.IdLeaf THEN f10.IdLeaf END AS 'Level10'
FROM
   (SELECT * FROM #Tax) f10
    JOIN #Tax f9 ON f9.l1 = f10.l1
                                 AND f9.l2 = f10.l2
                                 AND f9.l3 = f10.l3
                                 AND f9.l4 = f10.l4
                                 AND f9.l5 = f10.l5
                                 AND f9.l6 = f10.l6
                                 AND f9.l7 = f10.l7
                                 AND f9.l8 = f10.L8
                                 AND f9.l9 = f10.l9
                                 AND f9.l10 = 0 
       JOIN #Tax f8 ON f8.l1 = f10.l1
                                 AND f8.l2 = f10.l2
                                 AND f8.l3 = f10.l3
                                 AND f8.l4 = f10.l4
                                 AND f8.l5 = f10.l5
                                 AND f8.l6 = f10.l6
                                 AND f8.l7 = f10.l7
                                 AND f8.l8 = f10.l8
                                 AND f8.l9 = 0
                                 AND f8.l10 = 0 
        JOIN #Tax f7 ON f7.l1 = f10.l1
                    AND f7.l2 = f10.l2
                    AND f7.l3 = f10.l3
                    AND f7.l4 = f10.l4
                    AND f7.l5 = f10.l5
                    AND f7.l6 = f10.l6
                    AND f7.l7 = f10.l7
                    AND f7.l8 = 0
                    AND f7.l9 = 0
                    AND f7.l10 = 0 
       JOIN #Tax f6 ON f6.l1 = f10.l1
                    AND f6.l2 = f10.l2
                    AND f6.l3 = f10.l3
                    AND f6.l4 = f10.l4
                    AND f6.l5 = f10.l5
                    AND f6.l6 = f10.l6
                    AND f6.l7 = 0
                    AND f6.l8 = 0
                    AND f6.l9 = 0
                    AND f6.l10 = 0 
        JOIN #Tax f5 ON f5.l1 = f10.l1
                    AND f5.l2 = f10.l2
                    AND f5.l3 = f10.l3
                    AND f5.l4 = f10.l4
                    AND f5.l5 = f10.l5
                    AND f5.l6 = 0
                    AND f5.l7 = 0
                    AND f5.l8 = 0
                    AND f5.l9 = 0
                    AND f5.l10 = 0 
       JOIN #Tax f4 ON f4.l1 = f10.l1
                    AND f4.l2 = f10.l2
                    AND f4.l3 = f10.l3
                    AND f4.l4 = f10.l4
                    AND f4.l5 = 0
                    AND f4.l6 = 0
                    AND f4.l7 = 0
                    AND f4.l8 = 0
                    AND f4.l9 = 0
                    AND f4.l10 = 0
       JOIN #Tax f3 ON f3.l1 = f10.l1
                    AND f3.l2 = f10.l2
                    AND f3.l3 = f10.l3
                    AND f3.l4 = 0
                    AND f3.l5 = 0
                    AND f3.l6 = 0
                    AND f3.l7 = 0
                    AND f3.l8 = 0
                    AND f3.l9 = 0
                    AND f3.l10 = 0
       JOIN #Tax f2 ON f2.l1 = f10.l1
                    AND f2.l2 = f10.l2
                    AND f2.l3 = 0
                    AND f2.l4 = 0
                    AND f2.l5 = 0
                    AND f2.l6 = 0
                    AND f2.l7 = 0
                    AND f2.l8 = 0
                    AND f2.l9 = 0
                    AND f2.l10 = 0
       JOIN #Tax f1 ON f1.l1 = f10.l1
                    AND f1.l2 = 0
                    AND f1.l3 = 0
                    AND f1.l4 = 0
                    AND f1.l5 = 0
                    AND f1.l6 = 0
                    AND f1.l7 = 0
                    AND f1.l8 = 0
                    AND f1.l9 = 0
                    AND f1.l10 = 0
) Levels Order By L1,L2,L3,L4,L5,L6,L7,L8,L9,L10

0 个答案:

没有答案