PHP MySQL uses the rand function to randomly retrieve records
How to use a random number in MySQL and how to write a statement to update hundreds of Mysql Data!
To test the MySQL database, there is a database with tens of thousands of data records. How to Write a PHP file and update several hundred pieces of information each time? I write a loop to update one piece of information at a time, in this way, I can use while to write data. If an update is like 100 data records, how can I write it!
The correct answer is: Update cdb_posts set views = rand ();
By the way, I found an instance about MySQL rand function, as shown below:
In the INSERT command, Rand () is used in value (). If the field width is sufficient, it means that MySQL randomly queries several pieces of data.
Select * From 'table' order by rand () limit 5
You can.
However, the test results show that the efficiency is very low. It takes more than 8 seconds to query 5 data records in a database with more than 0.15 million entries
According to the official manual, Rand () is executed multiple times in the order by clause, which is naturally inefficient and inefficient.
You cannot use a column with RAND () values in an order by clause, because order by wowould evaluate the column multiple times.
Search for Google. Basically, data is randomly obtained by querying max (ID) * rand () on the Internet.
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;
However, five consecutive records are generated. The solution is to query only one item at a time and query five times. Even so, it is worthwhile because it takes less than 0.15 million seconds to query 0.01 tables.
The preceding statement uses join, Which is used on the MySQL forum.
Select *
From 'table'
Where ID> = (select floor (max (ID) * rand () from 'table ')
Order by ID limit 1;
I tested it. It took 0.5 seconds and the speed was good, but there was still a big gap with the above statements. I always feel that something is abnormal.
So I changed the statement.
Select * From 'table'
Where ID> = (select floor (RAND () * (select max (ID) from 'table ')))
Order by ID limit 1;
The query efficiency is improved, and the query time is only 0.01 seconds.
Finally, complete the statement and add the min (ID) judgment. At the beginning of the test, because I did not add the min (ID) Judgment, half of the time is always the first few rows in the table.
The complete query statement is:
Select * From 'table'
Where ID> = (select floor (RAND () * (select max (ID) from 'table')-(select Min (ID) from 'table ')) + (select Min (ID) from '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 (ID) from 'table') as ID) as T2
Where t1.id> = t2.id
Order by t1.id limit 1;
Finally, these two statements are queried 10 times in PHP respectively,
The former takes 0.147433 seconds.
The latter takes 0.015130 seconds.
It seems that using the join syntax is much more efficient than using functions directly in the WHERE clause.
Original address http://www.phpq.net/viewnews-69.html