title: How can I optimize these queries?
body: <p>Consider the following code snippet:</p>
  
  <pre><code>$beat = date('B'); // 1 beat = 86.4 seconds, 1000 beats = 1 day
  $total = 'SELECT COUNT(id) FROM ads WHERE featured = 1;'; // number of featured ads
  $current = 'SELECT * FROM ads  WHERE featured = 1 ORDER BY id ASC LIMIT 1 OFFSET ' . ($beat % $total) . ';'; // current featured ad
  </code></pre>
  
  <p>Basically this cycles trough all the featured ads giving each one of them a beat (86.4 seconds) window where they will be given special highlight, example:</p>
  
  <pre><code>$beat       $total          $current
  
  0           3           0
  1           3           1
  2           3           2
  3           3           0
  4           3           1
  5           3           2
  6           3           0
  7           3           1
  </code></pre>
  
  <p>This works just fine, however I was wondering if somehow it would be possible to eliminate the need for the $total query and accomplish the same using just one query.</p>
  
  <p>I don't see anyway to do this without using sub-queries but still, I'd like to hear your opinion on this.</p>