In our development of the efficiency has been a problem, especially for a lot of large data operations, today we ran into a random query data, at first we may think of the simplest order by RAND () to operate but the efficiency is not flattering ah.
Recently, because of the need to study the MySQL random extraction implementation method. For example, to randomly extract a record from the TableName table, the general wording would be: SELECT * from TableName ORDER by RAND () LIMIT 1.
There are two ways to achieve these results.
1. Create a new table with a number of numbers between 5 and 5. Use the ORDER by rand () to get the random number.
#建立指定范围数据表
The 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 to rand () limit 1
Advantages: Random numbers can specify some part of the data, do not need continuous.
Disadvantage: When the range of random numbers is very wide, it is more difficult to build a table.
2. Use MySQL's round () plus the rand () function to implement
#一句sql语句搞定
#auther: Xiao Qiang (Fortune Teller)
#date: 2008-03-31
The code is as follows
SELECT ROUND ((0.5-rand ()) *2*5)
#注释
#0.5-rand () can get 0.5 to +0.5 random numbers
# (0.5-rand ()) *2 can get 1 to +1 of random numbers
# (0.5-rand ()) *2*5 can get 5 to +5 of random numbers
#ROUND ((0.5-rand ()) *2*5) can get 5 to +5 random integers
But then I looked up the official MySQL manual, and the hint for rand () probably meant that the rand () function could not be used in an ORDER BY clause because it would cause data columns to be scanned multiple times. However, in the MySQL 3.23 version, it is still possible to implement random by the order by RAND ().
But the real test was found to be very inefficient. A library of more than 150,000, query 5 data, incredibly more than 8 seconds. View the official manual, also said that Rand () is executed multiple times in the ORDER BY clause, which is naturally inefficient and very low.
Search Google, the Internet is basically query Max (ID) * RAND () to randomly obtain data.
The code is as follows
SELECT * from ' table ' as T1 JOIN (select ROUND (RAND () * (SELECT MAX (ID) from ' table ') as ID) as T2 WHERE t1.id >= T1.id ASC LIMIT 5;
But this produces a continuous 5 record. The solution can only be one query at a time, query 5 times. Even so, because of the 150,000 table, the query only needs 0.01 seconds.
The following statements are used in Join,mysql forums where someone uses
The code is as follows
SELECT * from ' table ' WHERE ID >= (select FLOOR (MAX (ID) * RAND ()) From ' table ', ORDER by ID LIMIT 1;
I tested it, it takes 0.5 seconds, and the speed is good, but there is still a big gap with the above statement. There's always something wrong.
So I rewrote the statement.
The 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 is increased, the query time is only 0.01 seconds
Finally, the statement to improve, plus min (id) judgment. I was at the beginning of the test, because I did not add min (id) judgment, the result is half of the time always query to the first few lines in the table.
The full query statement is:
The code is as follows
SELECT * from ' table '
WHERE ID >= ((select MAX (ID) from ' table ') (select min (id) from ' table ') + (select min (id) from ') (select floor) Table '))
ORDER by ID LIMIT 1;
SELECT *
From ' table ' as T1 JOIN (select MAX (ID) from ' table ') + (select min (id) ' ROUND ') + (() + (ID) From ' table ') as ID) as T2
WHERE t1.id >= t2.id
ORDER by t1.id LIMIT 1;
Finally in PHP, the two statements are queried separately 10 times,
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 a where.
After many tests we have come to the conclusion that using the syntax of the join is much faster than the direct use in the where, there are better to submit friends can come out to chat.