To obtain the number of records at random, the simplest method in mysql must be orderbyrand (). However, this method can only be used when there are few table records. This is mainly because orderbyrand () causes usingfilesort. At this time, the query type will change to all, and the index will become invalid. The same effect can be achieved with simple modifications.
To obtain the number of records at random, the simplest method in mysql must be order by rand (). However, this method can only be used when there are few table records. This is mainly because order by rand () causes using filesort. At this time, the query type will change to all, and the index will become invalid. The same effect can be achieved with simple modifications.
Next I will use users (userId, userName, password ......) The table (with more than 1 million records) is used as an example to illustrate the efficiency of the following methods:
| The Code is as follows: |
|
|
1. select * from users order by rand () LIMIT 1 |
The execution of this SQL statement did not respond for a long time, and was forced to stop the execution manually. How can it hurt people! Later, I checked the MYSQL Manual. The prompt for RAND () indicates that the RAND () function cannot be used in the ORDER BY clause, this will cause the data column to be scanned multiple times, resulting in a very low efficiency! Inefficient. Do not use it!
| The Code is as follows: |
|
|
2. SELECT * FROM users AS t1 JOIN (select round (RAND () * (select max (userId) FROM 'users')-(select min (userId) FROM users )) + (select min (userId) FROM users) AS userId) AS t2 WHERE t1.userId> = t2.userId order by t1.userId LIMIT 1 |
It takes 0.031 seconds to execute this SQL statement. The efficiency is not mentioned, which is quite powerful! I thought it was so refreshing. Then, I changed "LIMIT 1" to "LIMIT 100" to randomly retrieve one hundred records, and the time was 0.048. But now the problem occurs, and it seems that the result is not random? In ORDER to verify the results, N times were executed again, which is not random. The problem occurs in order by t1.userId, Which is sorted BY userId. It is a good choice to randomly retrieve a record. It won't work if there are multiple records!
| The Code is as follows: |
|
|
3. SELECT * FROM users WHERE userId> = (select max (userId) FROM users)-(select min (userId) FROM users) * RAND () + (select min (userId) FROM users) LIMIT 1 |
It takes 0.039 s to execute this SQL statement, which is too efficient! Then I changed "LIMIT 1" to "LIMIT 10000", which took 0.063 s. After multiple verifications, my brother vowed to the light and the results would be random!
Conclusion: It is recommended to randomly retrieve one or more records!
4. obtain the maximum and minimum values through SQL, generate a random number randnum through php rand, and then pass
| The Code is as follows: |
|
| SELECT * FROM users WHERE userId> = randnum LIMIT 1 |
, The efficiency of getting a record should be good, but it should not work if there are multiple records.
Based on the record type, the two types are continuous and non-continuous.
Continuity refers to the continuous storage of records, and fields can prove that records are continuous, such as auto-increment IDs.
Non-continuous means that records are stored randomly. For example, the results of conditional queries are certainly not consecutive.
1. Continuous record Optimization
Obtain the maximum and minimum table IDs. Select max (id), min (id) from table
1. In the program, a random number between the maximum id and the minimum id is displayed. If the number is greater than the random number, a random record is recorded.
SQL code
| The Code is as follows: |
|
|
1. select * from table where id> intermediate number limit length;
Select * from table where id> intermediate number limit length; disadvantage: If the intermediate number is large, the number of required records cannot be obtained, and the randomness is not strong. |
| The Code is as follows: |
|
|
2. Random n median numbers of the maximum and minimum IDs in the program. When querying, use in to obtain the records of these median numbers.
SQL code
1. select * from table where id in (intermediate number 1, intermediate number 2, intermediate number 3)
Select * from table where id in (intermediate number 1, intermediate number 2, intermediate number 3) Note that if you want to obtain five records, we recommend that you random 10 records.
Disadvantage: the performance is not as good as 1st methods, but the randomness is stronger. |
Ii. non-continuous record Optimization
In fact, the non-continuous record method can be applied to the continuous record.
First obtain the total number of records, for example, select count (*) from table where groupid = 1;
Then random n numbers in the program less than the total number of records in the middle, and then through the loop
SQL code
| The Code is as follows: |
|
1. select * from table where groupid = 1 limit intermediate count, 1
Select * from table where groupid = 1 limit intermediate number, 1 to obtain records.
|
You can use prepare or union all to optimize cyclic SQL statements.
Conclusion: method 1 is inefficient and should not be used. A Random record is obtained. method 2 is a good choice, the JOIN syntax is more efficient than using functions directly in the WHERE clause, but method 3 is also good. Multiple records are randomly obtained.