MySQL ORDER BY Rand () efficiency

Source: Internet
Author: User
Tags manual join min mysql manual rand

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 wording would be: SELECT * from TableName ORDER by RAND () LIMIT 1.

However, I later looked at the official MySQL manual, where the hints for rand () probably meant that the rand () function could not be used in an ORDER BY clause because it would cause 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 ().

But the real test was found to be very inefficient. A library of more than 150,000, query 5 data, incredibly more than 8 seconds. View the official manual, also said that Rand () is executed multiple times in the ORDER BY clause, which is naturally inefficient and very low.

You cannot use a column with RAND () values in the clause, because order by would evaluate the column Mul Tiple times.

Searches Google and basically queries Max (ID) rand () to get data randomly.

Select *
from ' table ' as T1 JOIN (select ROUND (RAND () * (SELECT MAX (ID) from ' table ') as ID) as T2
Wher E t1.id >= t2.id
Order by t1.id ASC LIMIT 5;

But this results in a sequential 5 record. The solution can only be one query at a time, query 5 times. Even so, because of the 150,000 table, the query only needs 0.01 seconds. The following statement in

uses the

Select *
from ' table '
WHERE ID >= (SELECT FLOOR (MAX (ID) * RAND (JOIN,MYSQL) is used on the forums of the forum. )
Order by ID LIMIT 1;

I tested it, it takes 0.5 seconds, and it's pretty good, but there's still a big gap with the statement above. There's always something to think about.Side is not normal.

So I rewrote the statement.

Select * from "table"
WHERE ID >= (SELECT Floor (RAND () * (SELECT MAX (ID) from ' table '))
Order by ID LIMIT 1;

This, the efficiency is increased, the query time is only 0.01 seconds

Finally, then complete the statement, plus min (id) judgment. I was at the beginning of the test, because I did not add min (id) judgment, the result is half of the time is always query to the first few lines in the table.

The full query statement is:

Select * from ' table '
WHERE ID >= (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 (select MAX (ID) from ' table ')-(select MIN (id) from ' tabl E ')) + (SELECT MIN (ID) from ' table ') as ID) as T2
WHERE t1.id >= t2.id
Order by t1.id LIMIT 1;

The last two statements in PHP are queried 10 times,

The former takes 0.147433 seconds

The latter takes 0.015130 seconds

It seems that the syntax for join is more efficient than using the function directly in the where much higher.

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.