title: How to optimize a MySQL query so that a selected value in a WHERE clause is only computed once?
body: <p>I need to randomly select, in an efficient way, 10 rows from my table.</p>
  
  <p>I found out that the following works nicely (after the query, I just select 10 random elements in PHP from the 10 to 30 I get from the query):</p>
  
  <pre><code>   SELECT * FROM product WHERE RAND() &lt;= (SELECT 20 / COUNT(*) FROM product)
  </code></pre>
  
  <p>However, the subquery, though relatively cheap, is computed for every row in the table. How can I prevent that? With a variable? A join?</p>
  
  <p>Thanks!</p>