title: Fast select by category and price range in large mysql table
body: <p>I need to select a random product from large MySQL table(~9000000 rows, 4.5GiB) with specific category ID and price range.</p>
  
  <p>The solution I am using now takes about 30 seconds to complete request.</p>
  
  <pre><code>$query = mysqli_query($db, "SELECT MAX(id) FROM `products` WHERE category = $cat AND price &gt;= $price_min AND price &lt;= $price_max");
  $f = mysqli_fetch_array($query);
  $max_id = $f[0];
  $random_id = rand(0, $max_id);
  mysqli_free_result($query);
  
  $query = mysqli_query($db, "SELECT * FROM `products` id=$random_id LIMIT 1");
  $product = mysqli_fetch_assoc($query);
  </code></pre>
  
  <p>Is it possible to optimize the request?</p>