title: Space in SQL Server 2008 R2 slows down performance
body: <p>I have run into a rather weird problem. I have created the following query in SQL Server</p>
  
  <pre><code>SELECT * FROM leads.BatchDetails T1
  INNER JOIN leads.BatchHeader h ON T1.LeadBatchHeaderId = h.ID
  WHERE 
   T1.LeadBatchHeaderId = 34  
    AND (T1.TypeRC = 'R' OR h.DefaultTypeRC = 'R')    
   AND EXISTS (SELECT ID FROM leads.BatchDetails T2 where 
              T1.FirstName = T2.FirstName AND 
              T1.LastName = T2.LastName AND 
              T1.Address1 = T2.Address1 AND
              T1.City = T2.City AND                     
              T1.[State] = T2.[State] AND                     
              T1.Zip5 = T2.Zip5 AND                     
              T1.LeadBatchHeaderId = T2.LeadBatchHeaderId 
              and t2.ID &lt; t1.ID
              AND (T2.TypeRC = 'R' OR h.DefaultTypeRC = 'R' )
       )
  </code></pre>
  
  <p>It runs decently fast in 2 seconds. When formatting the code I accidently added an additional <code>SPACE</code> between <code>AND</code> + <code>EXISTS</code> so the query look like this.</p>
  
  <pre><code>SELECT * FROM leads.BatchDetails T1
  INNER JOIN leads.BatchHeader h ON T1.LeadBatchHeaderId = h.ID
  WHERE 
   T1.LeadBatchHeaderId = 34  
    AND (T1.TypeRC = 'R' OR h.DefaultTypeRC = 'R')    
   AND  EXISTS (SELECT ID FROM leads.BatchDetails T2 where 
              T1.FirstName = T2.FirstName AND 
              T1.LastName = T2.LastName AND 
              T1.Address1 = T2.Address1 AND
              T1.City = T2.City AND                     
              T1.[State] = T2.[State] AND                     
              T1.Zip5 = T2.Zip5 AND                     
              T1.LeadBatchHeaderId = T2.LeadBatchHeaderId 
              and t2.ID &lt; t1.ID
              AND (T2.TypeRC = 'R' OR h.DefaultTypeRC = 'R' )
       )
  </code></pre>
  
  <p>All of a sudden the query takes 13 seconds to execute.
  I am running SQL Server in an isolated sandbox environment and I have even tested it on a different sandbox. I also checked the executed query in profiler, the reads are virtually the same, but CPU time is way up</p>
  
  <p>If this is not weird enough, it's getting weirder. When I change <code>SELECT * FROM</code> to <code>SELECT Field1, ... FROM</code> at the top of the query the execution takes over 3 minutes.</p>
  
  <p>I have been working with SQL Server for 10 years and never seen anything like this. </p>
  
  <p>Edit:
  After following the suggestions below it appears that queries are "white-space-sensitive". However I still have no idea why the <code>SELECT * FROM</code> is so much faster than <code>SELECT Field1, ... FROM</code></p>