MySQL in rand () random query records efficiency issues and solutions to share

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

In our development of the efficiency has always been a problem, especially for a lot of large data volume operation, today we ran into a random query data, at first we might think of the simplest order by RAND () to operate but the efficiency is not flattering AH

Recently, due to the need for a general study of MySQL random extraction implementation method. For example, to randomly extract a record from the TableName table, the general notation is: SELECT * from TableName ORDER by RAND () LIMIT 1.
There are two ways to achieve these results.
1. Create a new table with a number between 5 and 5. Then the order by rand () is used to get the random number.
#建立指定范围数据表

Copy CodeThe code is as follows:
#auther: Xiao Qiang (Fortune Teller)
#date: 2008-03-31
CREATE TABLE Randnumber
Select-1 as Number
Union
Select-2
Union
Select-3
Union
Select-4
Union
Select-5
Union
Select 0
Union
Select 1
Union
Select 2
Union
Select 3
Union
Select 4
Union
Select 5

#得到随机数
#auther: Xiao Qiang (Fortune Teller)
#date: 2008-03-31
Select number
From Randnumber ORDER by rand () limit 1

Pros: A random number can specify a part of the data and does not need to be contiguous.
Cons: It is difficult to build a table when the range of random numbers is wide.
2. Use MySQL's round () plus the rand () function to implement
#一句sql语句搞定
#auther: Xiao Qiang (Fortune Teller)
#date: 2008-03-31
Copy CodeThe code is as follows:
SELECT ROUND ((0.5-rand ()) *2*5)
#注释
#0.5-rand () can get a random number from 0.5 to +0.5
# (0.5-rand ()) * * Can get a random number of 1 to +1
# (0.5-rand ()) *2*5 can get a random number of 5 to +5
#ROUND ((0.5-rand ()) *2*5) can get a random integer from 5 to +5

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.

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;

I tested, need 0.5 seconds, the speed is good, but with the above statement there is still a big gap. There is something wrong with the total sleep.
So I rewrote the sentence a bit.
Copy CodeThe code is as follows:
SELECT * from ' table '
WHERE ID >= (SELECT Floor (RAND () * (select MAX (ID) from ' table ')))
ORDER by ID LIMIT 1;

This, the efficiency has improved, the query time only 0.01 seconds
Finally, the statement is refined, plus 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 '
WHERE ID >= (SELECT Floor (RAND () * ((select MAX (ID) from ' table ')-(select min (id) from ' table ') + (select min (id) FRO M ' table ')))
ORDER by ID LIMIT 1;
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.
After many tests we came to the conclusion that using the syntax of join is much quicker than the direct use in the where, and there are better submissions of friends who can come out and talk.

MySQL in rand () random query records efficiency issues and solutions to share

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.