MySQL Forums
Forum List  »  MyISAM

Re: order by rand() Query takes too long (writes to disk)
Posted by: Rick James
Date: October 30, 2012 12:22PM

Locking -- switch to InnoDB.

ORDER BY RAND() reads the entire table -- hence, the slowness.

> Is it possible to do a smarter and faster query to solve this problem, or is there any idea to tweak the performance?

Such a deal I have for you! http://mysql.rjweb.org/doc.php/random

This would also help:
SELECT  x.*
    FROM  
      ( SELECT  *
            FROM  table
            WHERE  external_link_count < 50
            order by  rand()) x
    GROUP BY  SUBSTRING_INDEX(url,'/',3) ; 
-->
SELECT  y.*
    FROM  
      ( SELECT  id
            FROM  table
            WHERE  external_link_count < 50
            order by  rand()) x
    JOIN  table y ON y.id = x.id
    GROUP BY  SUBSTRING_INDEX(y.url,'/',3);
That is, have the subquery find only the PRIMARY KEY, not the whole row.

For further discussion, please provide SHOW CREATE TABLE

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: order by rand() Query takes too long (writes to disk)
1946
October 30, 2012 12:22PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.