Skip to content

Commit

Permalink
Optimised the RAND() SELECT query
Browse files Browse the repository at this point in the history
  • Loading branch information
auxesis committed Sep 8, 2011
1 parent 51eb46a commit ddc85ed
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/common.inc.php
Expand Up @@ -51,11 +51,17 @@ function get_number_of_actors() {

function get_random_movie() {

$result = mysql_query_wrapper("SELECT * FROM title WHERE title != ''
AND kind_id = 1
ORDER BY RAND() LIMIT 1");
return mysql_fetch_assoc($result);
# "adding holes to the numbers" section in
# http://jan.kneschke.de/projects/mysql/order-by-rand/
$result = mysql_query_wrapper("
SELECT * FROM title AS t1 JOIN
(SELECT (RAND() * (SELECT MAX(id) FROM title)) AS id) AS t2
WHERE t1.id >= t2.id
ORDER BY t1.id ASC
LIMIT 1
");

return mysql_fetch_assoc($result);
}

function get_random_actor() {
Expand Down

0 comments on commit ddc85ed

Please sign in to comment.