About the efficiency of randomly extracting order by Rand (), and how to improve it!

Source: Internet
Author: User

Recently, because of the need to study the MySQL random extraction implementation method.

For example, to randomly extract a record from the TableName table, the general way to write is
SELECT * FROM TableName ORDER by RAND () LIMIT 1

But then I looked at the official MySQL manual, which was given to Rand () with the following hints:
You can ' t use a column with RAND () values in the clause, because order by would evaluate the "column multiple times . In MySQL Version 3.23, with can, however, do:select * FROM table_name ORDER BY RAND ()
Presumably it means that the rand () function cannot be used in an ORDER BY clause because it causes data columns to be scanned multiple times. However, in the MySQL 3.23 version, it is still possible to implement random by the order by RAND ().

And then I tried the possibility that the order by RAND () could be executed on my own version 4.0, but could not be performed on the company's 3.x (specifically forgotten) and seemed a little different from the official manual.

Later on the Internet to find some information to understand that,
SELECT * FROM TableName ORDER by RAND () LIMIT 1
Scans the entire table and then randomly returns a record. This is useful for smaller tables, usually not more than 300,000 rows of records. But if the record is greater than 300,000 lines, the process becomes very slow ...

So, the conclusion is, I suggest that you don't need to be able to use the order by RAND (). Because of this can avoid the future table data volume caused by inefficient, and can avoid some version of MySQL does not support this type of writing.
Finally, the main ideas of a more practical alternative method are given:
Assuming the ID is the primary key
First: SELECT MIN (ID), MAX (ID) from TableName
Then: $id =rand ($min, $max); Returns an ID number between the maximum ID and the minimum ID that was just fetched through Rand.
Finally: SELECT * from tablename WHERE id= ' $id ' LIMIT 1
If the ID number produced by the auto increment, there may be an ID column has been deleted, resulting in the maximum and the smallest ID between the discontinuity, where you can first determine the random generated by this ID number exists.

Some people are sure to ask: if not a random search, but to randomly retrieve more than one, such as 10 how to do ...

The simplest is to take a contiguous range, such as
SELECT * FROM tablename WHERE ID > ' $id ' LIMIT
But this method is randomly sequential.

Then, for example, generate a Where id = ' {$id [' 1 ']} ' or id = ' {$id [' 2 ']} ' or .... clause, write a few more sentences, $id the value randomly generated number.
But this method, if more than a clause, such as hundreds of words. In that case, the SQL statement will be larger and will also affect efficiency.

Also, first randomly select a range, such as
Select ID from tablename WHERE ID > ' $id ' LIMIT
and then randomly select 10

in the result. Take one at a time randomly, take 10 times.

The specific situation to see which is appropriate.
I can think of so much first.

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.