MySQL Query of random data in four ways and performance comparison _ MySQL

Source: Internet
Author: User
MySQL queries random data in four ways and performance comparison below from the following four solutions to analyze their respective advantages and disadvantages.
Solution 1:
SELECT * FROM 'table' order by rand () LIMIT 0, 1;
This method is very slow. The reason is that MySQL creates a zero-time table to store all result sets, then creates a random index for each result, sorts the results, and returns the results.
There are several ways to make it faster.
The basic idea is to first obtain a random number and then use this random number to obtain the specified row.
Since all rows have a unique id, we will only take the random number between the minimum and the maximum id, and then get the id as this number of rows. To make this method effective when the id is not consecutive, we use "> =" instead of "=" in the final query ".
To obtain the minimum and maximum IDs of the entire table, we use the MAX () and MIN () aggregate functions. The two methods return the maximum and minimum values in the specified group. Here, this group is the value of all id fields in our table.
Solution 2:
$ Range_result = mysql_query ("select max ('id') AS max_id, MIN ('id') AS min_id FROM 'table '");
$ Range_row = mysql_fetch_object ($ range_result );
$ Random = mt_rand ($ range_row-> min_id, $ range_row-> max_id );
$ Result = mysql_query ("SELECT * FROM 'table' WHERE 'id'> = $ random LIMIT 0, 1 ");
As we mentioned earlier, this method limits each row of the table with a unique id value. So what if this is not the case?
The following solution uses the MySQL LIMIT clause. LIMIT receives two parameter values. The first parameter specifies the offset of the first row of the returned result, and the second parameter specifies the maximum number of rows of the returned result. The offset specifies that the first row is 0 rather than 1.
To calculate the offset of the first line, we use the RAND () method of MySQL to generate a random number from 0 to 1. Then we Multiply this number by the number of records in the inverted table obtained using the COUNT () method. Since the LIMIT parameter must be int type but not float, we use FLOOR () to process the result. FLOOR () calculates the value smaller than the maximum value of the expression. The final code is as follows:
Solution 3:
$ Offset_result = mysql_query ("select floor (RAND () * COUNT (*) AS 'offset 'FROM 'table '");
$ Offset_row = mysql_fetch_object ($ offset_result );
$ Offset = $ offset_row-> offset;
$ Result = mysql_query ("SELECT * FROM 'table' LIMIT $ offset, 1 ");
After MySQL 4.1, we can use subqueries to merge the above two methods:
Solution 4:
SELECT * FROM 'table' WHERE id> = (select floor (MAX (id) * RAND () FROM 'table') order by id LIMIT 1;
This solution has the same weakness as solution 2 and is only valid for tables with unique id values.
Remember the reason why we initially looked for an alternative method to select random rows. speed! So what will happen to the comparison of the execution time of these solutions? I will not point out hardware and software configurations or give specific numbers. The approximate result is as follows:
Solution 1 is the slowest (we assume it takes 100% of the time ).
Solution 2 uses 79%
Solution 3

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.