MySQL random extract query MySQL Order by Rand () efficiency issue

Source: Internet
Author: User
Tags mysql manual rand time 0

MySQL random query: MySQL Order by Rand () efficiency problem has always been a developer's frequently asked questions, we are not DBA, not so cow B, can only slowly study, recently due to project issues, need to probably study the MySQL random extraction implementation method

To randomly extract a record from the TableName table, the general notation is: SELECT * from TableName ORDER by RAND () LIMIT 1.

But then I checked the official MySQL manual, and the Hints for rand () probably mean that the rand () function cannot be used in an ORDER BY clause because it causes the data column to be scanned multiple times. However, in MySQL version 3.23, the Order by RAND () can still be implemented randomly.

But the real test is that it's very inefficient. A library of more than 150,000, query 5 data, incredibly more than 8 seconds. Check out the official manual, which also says Rand () is executed several times in the ORDER BY clause, naturally efficient and very low.

Copy CodeThe code is as follows:
You cannot use a column with RAND () values in an ORDER BY clause, because ORDER BY
Would evaluate the column multiple times.


Searching for Google, the internet is basically querying max (ID) * RAND () to get data randomly.

Copy CodeThe code is as follows:
SELECT * from ' table ' as T1 joins (select ROUND (RAND () * (select MAX (ID) from ' table ')) as ID) as T2 WHERE t1.id >= t2.i D ORDER by T1.id ASC LIMIT 5;


However, this will produce 5 consecutive records. The solution can only be one query at a time, query 5 times. Even so it is worth it, because 150,000 of the tables, the query only need 0.01 seconds less than.
The following statement uses a join,mysql forum where someone uses

Copy CodeThe code is as follows:
SELECT * from ' table ' WHERE ID >= (SELECT Floor (MAX (ID) * RAND ()) from ' table ') the ORDER by ID of LIMIT 1;


Then refine the statement, plus the min (id) judgment. When I first tested it, it was because I didn't add the min (id) judgment, and half the time I always queried the previous rows in the table.
The complete query statement is:

Copy CodeThe code is as follows:
SELECT * from ' table ' 2 WHERE ID >= (The Select Floor (RAND () * ((select MAX (ID) from ' table ')-(select MIN (ID) from ' table ') )) + (SELECT MIN (ID) from ' table ')) 3 ORDER by ID LIMIT 1;

Copy CodeThe code is as follows:
SELECT *
From ' table ' as T1 JOIN (select ROUND (RAND () * ((select MAX (ID) from ' table ')-(select min (id) from ' table ') + (select min (i d) from ' table ') as ID) as T2
WHERE t1.id >= t2.id
ORDER by T1.id LIMIT 1;


Finally, the two statements were queried 10 times in PHP,
The former takes 0.147433 seconds
The latter takes time 0.015130 seconds
It seems that the syntax for join is much higher than the efficiency of using functions directly in the where.

MySQL random extract query MySQL Order by Rand () efficiency issue

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.