title: Getting very different results for statistics timing in SQL Server
body: <p>I am trying to time/benchmark a query as part of a bigger performance analysis.</p>
  
  <p>In SQL, I have enabled timing via <code>SET statistics time ON</code> and the query I am timing looks like <code>SELECT A, B from foo where id = 123</code>. Before each query, I execute:</p>
  
  <pre><code>DBCC DROPCLEANBUFFERS
  DBCC FREEPROCCACHE
  </code></pre>
  
  <p>Below are the results of 2 runs (not subsequently):</p>
  
  <p><strong>Run 1:</strong></p>
  
  <pre><code>SQL Server parse and compile time: 
     CPU time = 0 ms, elapsed time = 8 ms.
  SQL Server parse and compile time: 
     CPU time = 0 ms, elapsed time = 0 ms.
  
  (1212 row(s) affected)
  
   SQL Server Execution Times:
     CPU time = 0 ms,  elapsed time = 15 ms.
  </code></pre>
  
  <p><strong>Run 2:</strong></p>
  
  <pre><code>SQL Server parse and compile time: 
     CPU time = 0 ms, elapsed time = 3 ms.
  SQL Server parse and compile time: 
     CPU time = 0 ms, elapsed time = 0 ms.
  
  (1212 row(s) affected)
  
   SQL Server Execution Times:
     CPU time = 16 ms,  elapsed time = 14 ms.
  </code></pre>
  
  <p>What I don't understand is:</p>
  
  <ol>
  <li>Why are there 2 'parse and compile time' sections and why are the results different within the same query?</li>
  <li>Are the parse and compile time included in the CPU time below them</li>
  <li>Why is the CPU time 0 in 'run 1'</li>
  <li>Why is the CPU time more than the elapsed time in 'run 2'</li>
  </ol>
  
  <p>It is not very clear what number provides the CPU <strong>User</strong> time, that is how long did my query actually take which should be at most the elapsed time.</p>
  
  <p>On the bigger picture, I have a data intensive application that reads only from the database - each query is fast but it can query for a lot of data and it does this via individual queries, not in bulk. Is it a problem that I am trying to time an individual query - should I construct an sql script which takes a more meaningful time than a few milliseconds? To me the answer is 'Yes' but I am worried about not understanding the behaviour on a microscopic test before moving to a macroscopic test.</p>