PHP MySQL uses the RAND function to randomly fetch records (GO)

Source: Internet
Author: User
Tags php mysql

PHP MySQL uses the RAND function to fetch records randomly

How to use random numbers in MySQL, how to write a statement can update hundreds of MySQL data! Need to test MySQL database, there is a database of tens of thousands of data, how to write a PHP file each time update hundreds of information, I am writing a cycle update a message, so I know with while write can be, if one update is like 100 data change how to write it!

The correct answer is: UPDATE cdb_posts SET views = rand (); By the way, I got you some examples of the MySQL rand function, as follows: In the Insert command, value () is in rand (), notice that the field width is sufficient to always think that MySQL randomly queries a few data, with SELECT * from ' table ' ORDER by RAND () LIMIT 5

You can do it. 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 to view the official manual, also said Rand () in the ORDER BY clause will be executed several times, natural efficiency and very low. You cannot use a column with RAND () values in an ORDER BY clause, because order by would evaluate the column multiple time S.

Searching for Google, the internet is basically querying max (ID) * RAND () to get data randomly. 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 above statement uses the 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, 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. SELECT * from ' table ' WHERE ID >= (The Select Floor (RAND () * (select MAX (ID) from ' table '))) ORDER by ID LIMIT 1;

This, the efficiency is improved, the query time only 0.01 seconds finally, the statement perfect, 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 full query statement is: SELECT * from ' table ' WHERE ID >= (The 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 ') + (SEL ECT MIN (ID) from ' table ')) as-id) as T2 WHERE t1.id >= t2.id ORDER by t1.id LIMIT 1;

At last, the two statements are queried 10 times in PHP, the former takes 0.147433 seconds and the latter takes 0.015130 seconds. It seems that the syntax of join is much higher than using the function directly in the where.

Original Address http://www.phpq.net/viewnews-69.html

PHP MySQL uses the RAND function to randomly fetch records (GO)

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.