title: Is COUNT faster than pulling the records and counting in code?
body: <p>Here is the situation:</p>
  <ol>
  <li><p>I first need to run a query to know how many records exist.</p>
  <p>For example: <code>SELECT COUNT(DISTINCT userid) from users;</code></p>
  </li>
  <li><p>Often this will be all that's needed.  However, sometimes (say 30% of the time) following the first query, the user will want to run a second query, detailing the records.</p>
  <p>For example: <code>SELECT * FROM users;</code></p>
  </li>
  </ol>
  <p>Is there any reason to run <code>SELECT COUNT</code> initially instead of just <code>SELECT</code>?  That is, is making the count of records in SQL faster than actually pulling the records back?  Or is it doing essentially the same work either way and so I should avoid doing two queries?</p>
  <p>In other words, is it better to just always pull the records in the first query (not use <code>COUNT</code>), then count the records in code (Java).  If the user wants to run the second query, then great, I already have the data.  If not, then just dump it.</p>
  <p>What's the best practice here?</p>