random data selection from MySQL is also our most commonly used hair, the simplest way is to use "ORDER by rand ()", this article describes the order by rand (), including 4 ways to obtain random data, and analyze their advantages and disadvantages. the pros and cons of each of the following four scenarios are analyzed below.
Programme One:
Copy the Code code as follows:SELECT * from ' table ' ORDER by RAND () LIMIT 0,1;
the problem with this approach is that it is very slow. The reason is because MySQL creates a zero-day table to hold all the result sets, then gives each result a random index, and then sorts and returns.
There are several ways to get it up.
The basic idea is to get a random number and then use that random number to get the specified row.
since all rows have a unique ID, we will only take the random number between the minimum and maximum IDs, and then get the ID for this number of lines. In order for this method to be effective when the ID is discontinuous, we use ">=" instead of "=" in the final query.
to get the minimum and maximum IDs for the entire table, we use the Max () and Min () two aggregate functions. Both methods return the maximum and minimum values in the specified group. Here This group is the value of all the ID fields in our table.
Scenario Two:
Copy the Code code as follows:<?php
$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 just mentioned, this method restricts each row of the table with a unique ID value. So what if it's not the case?
The following scenario 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 to return the result. The offset specifies that the first row is 0 instead of 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 table records we get by using the count () method. Since the parameter of limit must be of type int and cannot be float, we use floor () to process the result. Floor () calculates the maximum value that is less than the expression. The final code is this:
Programme III:
Copy the Code code as follows:<?php
$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 sub-subqueries to merge the above two methods:
Programme IV:
Copy the Code code as follows:select * from ' table ' WHERE ID >= (SELECT Floor (MAX (ID) * RAND ()) from ' table ') the ORDER by ID of LIMIT 1;
This scenario has the same weaknesses as scenario two, only valid for tables with unique ID values.
Remember the reason that we initially looked for alternative ways to select random lines, speed! So what happens to the execution time comparisons of these scenarios? I will not point out hardware and software configurations or give specific numbers. The approximate result is this:
The slowest is the solution one (we assume it takes 100% of the time).
scenario two uses 79%.
Plan three –13%
Scenario Four –16%
So, plan three wins!
4 Ways and performance comparison of MySQL query random data