title: Quickly select random ID from mysql table with millions of non-sequential records
body: <p>I've looked around and there doesnt seem to be any easy way to do this. It almost looks like it's easier just to grab a subset of records and do all the randomizing in code (perl). The methods I've seen online seem like theyre geared more to at most hundreds of thousands, but certainly not millions.</p>
  
  <p>The table I'm working with has 6 million records (and growing), the IDs are auto incremented, but not always stored in the table (non-gapless).</p>
  
  <p>I've tried to do the LIMIT 1 query that's been recommended, but the query takes forever to run  -- is there a quick way to do this, given that there are gaps in the record? I can't just take the max and randomize over the range.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>One idea I had maybe was to grab the max, randomize a limit based on the max, and then grab a range of 10 records from random_limit_1 to random_limit_2 and then taking the first record found in that range.</p>
  
  <p>Or if I know the max, is there a way i can just pick say the 5th record of the table, without having to know which ID it is. Then just grabbing the id of that record.</p>
  
  <p><strong>Update:</strong></p>
  
  <p>This query is somewhat faster-ish. Still not fast enough =/</p>
  
  <pre><code>SELECT t.id FROM table t JOIN (SELECT(FLOOR(max(id) * rand())) as maxid FROM table) as tt on t.id &gt;= tt.maxid LIMIT 1
  </code></pre>