title: TABLESAMPLE returns wrong number of rows?
body: <p>I've just discovered the <a href="http://msdn.microsoft.com/en-us/library/ms189108%28v=sql.90%29.aspx" rel="noreferrer"><code>TABLESAMPLE</code></a> clause but surprisingly it doesn't return the number of rows i've specified.</p>
  
  <p>The table that i've used has ~14M rows and i wanted an arbitrary sample of 10000 rows.</p>
  
  <pre><code>select * from tabData TABLESAMPLE(10000 ROWS)
  </code></pre>
  
  <p>I get not 10000 but a different number everytime i execute it(between 8000 and 14000). </p>
  
  <p>What's going on here, have i misunderstood the intended purpose of <code>TABLESAMPLE</code>?</p>
  
  <p><strong>Edit</strong>: </p>
  
  <p><a href="http://www.mssqltips.com/sqlservertip/1308/retrieving-random-data-from-sql-server-with-tablesample/" rel="noreferrer">David's link</a> explains it pretty well.</p>
  
  <p>This returns always 10000 roughly random rows in an efficient way:</p>
  
  <pre><code>select TOP 10000 * from tabData TABLESAMPLE(20000 ROWS);
  </code></pre>
  
  <p>and the <code>REPEATABLE</code> option helps to get always the same (unless data has changed)</p>
  
  <pre><code>select TOP 10000 * from tabData TABLESAMPLE(10000 ROWS) REPEATABLE(100);
  </code></pre>
  
  <p>Since i wanted to know if it's more expensive to use <code>TABLESAMPLE</code> with a large number of rows to ensure(?) that i get the correct row-number, i've measured it;</p>
  
  <p>1.loop (20 times):</p>
  
  <pre><code>select TOP 10000 * from tabData TABLESAMPLE(10000 ROWS);
  
  (9938 row(s) affected)
  (10000 row(s) affected)
  (9383 row(s) affected)
  (9526 row(s) affected)
  (10000 row(s) affected)
  (9545 row(s) affected)
  (9560 row(s) affected)
  (9673 row(s) affected)
  (9608 row(s) affected)
  (9476 row(s) affected)
  (9766 row(s) affected)
  (10000 row(s) affected)
  (9500 row(s) affected)
  (9941 row(s) affected)
  (9769 row(s) affected)
  (9547 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (9478 row(s) affected)
  First batch(only 10000 rows) completed in: 14 seconds!
  </code></pre>
  
  <p>2.loop (20 times):</p>
  
  <pre><code>select TOP 10000 * from tabData TABLESAMPLE(10000000 ROWS);
  
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  (10000 row(s) affected)
  Second batch(max rows) completed in: 13 seconds!
  </code></pre>
  
  <p>3.loop:  counterscheck with 100% random rows using ORDER BY NEWID():</p>
  
  <pre><code>select TOP 10000 * from tabData ORDER BY NEWID();
  
  (10000 row(s) affected)
  </code></pre>
  
  <p>Cancelled after one execution that lasted <strong>23 minutes</strong></p>
  
  <p><strong>Conclusion</strong>:</p>
  
  <p>So suprisingly the approach with an exact <code>TOP</code> clause and a large number in <code>TABLESAMPLE</code> is <strong>not</strong> slower. Hence it's a very efficient alternative to <code>ORDER BY NEWID()</code> if it doesn't matter that the rows are not random per row but per page level(Each 8K page for the table is given a random value).</p>