MySQL random query data and performance analysis

Source: Internet
Author: User
Tags rand

For example, there is a requirement, through the SQL statement, to return a random integer of 5 to 5. If this one is in PHP, it is very simple and straightforward
<?php
Print rand ( -5,5);
?>
In MySQL, the RAND function can have only one parameter. Excerpt from Manual
RAND () rand (N)
Returns a random floating-point value of V, ranging from 0 to 1 (that is, the range is 0≤v≤1.0). If an integer parameter N is specified, it is used as a seed value to produce a repeating sequence.

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.
#建立指定范围数据表
#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

SELECT ROUND ((0.5-rand ()) *2*5)

#注释
#0.5-rand () can get 0.5 to +0.5 of 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 a random integer of 5 to +5

Advantages: When the range of random numbers is very wide, only need to change the *5 5, very convenient.
Disadvantage: Random numbers can only be contiguous, you can not specify a part of the data.


In MySQL, query 5 data without duplication, using the following:

SELECT * from ' table ' ORDER by RAND () LIMIT 5

It's OK. 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

Search Google, the Internet is basically query Max (ID) * RAND () to randomly obtain data.

SELECT *
From ' table ' as T1 JOIN (select ROUND (RAND () * (SELECT MAX (ID) from ' table ') as ID) as T2
WHERE t1.id >= t2.id
ORDER BY T1.id ASC LIMIT 5;

But this will produce a continuous 5 records. 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 above statement uses a join,mysql forum where someone uses

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 is something wrong with the total sleep.

So I rewrote the statement.

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 the 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 is always query to the first few lines in the table.
The full query statement is:

  code is as follows copy code


  SELECT * from ' table '
 where ID >= (select Floor () (select MAX (ID) from ' table ')-(select MIN (ID) FRO M ' table ') + (Selectmin (ID) from ' table ')]  
 order by ID LIMIT 1;
 
SELECT *
 from ' Table ' as T1 JOIN (select MAX (ID) from ' table ') + (select min (id) ' ROUND ') + ("select" (ID) FR OM ' table ') as ID) as T2
 where t1.id >= t2.id
Order by t1.id LIMIT 1;

&NBSP
The last two statements are queried 10 times,
The former takes 0.147433 seconds
The latter takes 0.015130 seconds
It appears that the syntax for join is much higher than the efficiency of using the function directly in the where.

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.