How to implement random query of MySQL

Source: Internet
Author: User
Tags mysql manual time 0

SELECT *
From ' table ' as T1 JOIN (select ROUND (RAND () * ((select MAX (ID) from ' table ')-(select min (id) from ' table ') + (select min (i d) from ' table ') as ID) as T2
WHERE t1.id >= t2.id
ORDER by T1.id LIMIT 1;

MySQL's random extraction implementation method. For example, to randomly extract a record from the TableName table, the general notation is: SELECT * from TableName ORDER by RAND () LIMIT 1.

But then I checked the official MySQL manual, and the Hints for rand () probably mean that the rand () function cannot be used in an ORDER BY clause because it causes the data column to be scanned multiple times. However, in MySQL version 3.23, the Order by RAND () can still be implemented randomly.

But the real test is that it's very inefficient. A library of more than 150,000, query 5 data, incredibly more than 8 seconds. Check out the official manual, which also says Rand () is executed several times in the ORDER BY clause, naturally efficient and very low.
You cannot use a column with RAND () values in an ORDER BY clause, because order by would evaluate the column multiple time S.
Searching for Google, the internet is basically querying max (ID) * RAND () to get data randomly.
SELECT *
From ' table ' as T1 joins (select ROUND (RAND () * (select MAX (ID) from ' table ')) as ID) as T2
WHERE t1.id >= t2.id
ORDER by T1.id ASC LIMIT 5;

However, this will produce 5 consecutive records. The solution can only be one query at a time, query 5 times. Even so it is worth it, because 150,000 of the tables, the query only need 0.01 seconds less than.

The following statement uses a join,mysql forum where someone uses
SELECT *
From ' table '
WHERE ID >= (SELECT floor (MAX (ID) * RAND ()) from ' table ')
ORDER by ID LIMIT 1;

I tested, need 0.5 seconds, the speed is good, but with the above statement there is still a big gap. There is something wrong with the total sleep.

So I rewrote the sentence a bit.
SELECT * from ' table '
WHERE ID >= (SELECT Floor (RAND () * (select MAX (ID) from ' table ')))
ORDER by ID LIMIT 1;

This, the efficiency has improved, the query time only 0.01 seconds

Finally, the statement is refined, plus min (id) judgment. When I first tested it, it was because I didn't add the min (id) judgment, and half the time I always queried the previous rows in the table.
The complete query statement is:
SELECT * from ' table '
WHERE ID >= (SELECT Floor (RAND () * ((select MAX (ID) from ' table ')-(select min (id) from ' table ') + (select min (id) FRO M ' table ')))
ORDER by ID LIMIT 1;

SELECT *
From ' table ' as T1 JOIN (select ROUND (RAND () * ((select MAX (ID) from ' table ')-(select min (id) from ' table ') + (select min (i d) from ' table ') as ID) as T2
WHERE t1.id >= t2.id
ORDER by T1.id LIMIT 1;

Finally, the two statements were queried 10 times in PHP,
The former takes 0.147433 seconds
The latter takes time 0.015130 seconds
It seems that the syntax for join is much higher than the efficiency of using functions directly in the where.

How to implement random query of MySQL

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.