title: How to properly index sql server table to increase query performance
body: <p>i need to add correct indexes to a temp table to speed up a query. according to msdn i check the menu "Include Actual Execution Plan" and run the query in SSMC, after the execution i can see the query plan but there's no indication about the indexes to create.</p>
  
  <p>in the table @Tax the fields l1,l2....l10 identify the position of the element within a hierarchical structure:</p>
  
  <p>the dummy data in the sample is representing the following structure:</p>
  
  <pre><code>--node idleaf=1
  ----child idleaf=3 of 1
  --------child idleaf=5 of 3
  ----child idleaf=4 of 1
  --node idleaf=2
  </code></pre>
  
  <p>with the tremendous query i need to extract the list of all parents of each record starting form the top
  ex: </p>
  
  <p>this is the result of the query (omitting some field is not interesting for the sample)
  idleaf,description,node_path</p>
  
  <pre><code>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_________
  </code></pre>
  
  <p>query:</p>
  
  <pre><code>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&gt;0 Then Levels.IdLeaf9
      When Levels.Level9&gt;0 Then Levels.IdLeaf8
      When Levels.Level8&gt;0 Then Levels.IdLeaf7
      When Levels.Level7&gt;0 Then Levels.IdLeaf6
      When Levels.Level6&gt;0 Then Levels.IdLeaf5
      When Levels.Level5&gt;0 Then Levels.IdLeaf4
      When Levels.Level4&gt;0 Then Levels.IdLeaf3
      When Levels.Level3&gt;0 Then Levels.IdLeaf2
      When Levels.Level2&gt;0 Then Levels.IdLeaf1
      When Levels.Level1&gt;0 Then null
  END AS IdParent,
  CASE 
      When Levels.Level10&gt;0 Then 10
      When Levels.Level9&gt;0 Then 9
      When Levels.Level8&gt;0 Then 8
      When Levels.Level7&gt;0 Then 7
      When Levels.Level6&gt;0 Then 6
      When Levels.Level5&gt;0 Then 5
      When Levels.Level4&gt;0 Then 4
      When Levels.Level3&gt;0 Then 3
      When Levels.Level2&gt;0 Then 2
      When Levels.Level1&gt;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 &lt;&gt; f1.IdLeaf THEN f2.IdLeaf END AS 'Level2',
       CASE WHEN f3.IdLeaf &lt;&gt; f2.IdLeaf THEN f3.IdLeaf END AS 'Level3',
       CASE WHEN f4.IdLeaf &lt;&gt; f3.IdLeaf THEN f4.IdLeaf END AS 'Level4',
       CASE WHEN f5.IdLeaf &lt;&gt; f4.IdLeaf THEN f5.IdLeaf END AS 'Level5',
       CASE WHEN f6.IdLeaf &lt;&gt; f5.IdLeaf THEN f6.IdLeaf END AS 'Level6',
       CASE WHEN f7.IdLeaf &lt;&gt; f6.IdLeaf THEN f7.IdLeaf END AS 'Level7',
       CASE WHEN f8.IdLeaf &lt;&gt; f7.IdLeaf THEN f8.IdLeaf END AS 'Level8',
       CASE WHEN f9.IdLeaf &lt;&gt; f8.IdLeaf THEN f9.IdLeaf END AS 'Level9',
       CASE WHEN f10.IdLeaf &lt;&gt; 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
  </code></pre>