title: How can i optimize MySQL's ORDER BY RAND() function?
body: <p>I'd like to optimize my queries so I look into <code>mysql-slow.log</code>.</p>
  
  <p>Most of my slow queries contains <code>ORDER BY RAND()</code>. I cannot find a real solution to resolve this problem. Theres is a possible solution at <a href="http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/#comment-2184" rel="noreferrer">MySQLPerformanceBlog</a> but I don't think this is enough. On poorly optimized (or frequently updated, user managed) tables it doesn't work or I need to run two or more queries before I can select my <code>PHP</code>-generated random row.</p>
  
  <p>Is there any solution for this issue?</p>
  
  <p>A dummy example:</p>
  
  <pre><code>SELECT  accomodation.ac_id,
          accomodation.ac_status,
          accomodation.ac_name,
          accomodation.ac_status,
          accomodation.ac_images
  FROM    accomodation, accomodation_category
  WHERE   accomodation.ac_status != 'draft'
          AND accomodation.ac_category = accomodation_category.acat_id
          AND accomodation_category.acat_slug != 'vendeglatohely'
          AND ac_images != 'b:0;'
  ORDER BY
          RAND()
  LIMIT 1
  </code></pre>