A method for implementing random numbers in the MySQL rand function _mysql

Source: Internet
Author: User
Tags rand time 0

Need to test the MySQL database, which has a database of tens of thousands of data, how to write a PHP file every time update hundreds of information, I am writing a cycle to update a message, so I know with a while to write on it, if an update like 100 data changes how to write it! The correct answer is to use the MySQL rand function: UPDATE cdb_posts SET views = rand (); and I got you some examples of the MySQL rand function, as follows: Then in the Insert command, value () is used with Rand (), Note that the field width is long enough to assume that MySQL randomly queries a few data, use 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 to view the official manual, also said rand () placed in the ORDER BY clause will be executed many times, natural efficiency and very low.

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 &G t;= 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, efficiency and improve, query time only 0.01 seconds finally, then 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 is always query to the first few lines in the table.
The full query statement is:

SELECT * from ' table ' 
WHERE ID >= Select Floor (RAND () * (select MAX (ID) from ' table ')-(select MIN (id) from ' tabl E ') + (SELECT MIN (ID) from ' table ')), order by 
ID LIMIT 1;
SELECT * from ' table ' as T1 JOIN (select ROUND ("SELECT MAX (ID) from ' table ')" (select 
MIN (ID) from ' table ')) + (SELECT MIN (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

The above is the method of implementing random numbers in the MySQL rand function.

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.