Mysql optimizes the method of fetching random data slowly _mysql

Source: Internet
Author: User
Tags manual mysql manual time 0
The day before yesterday because the work required me to take a random few records from a database of 5W records, here I am directly using the MySQL rand by function to direct, thousands of records okay, but if the tens of thousands of feeling to a few seconds, this is very slow, the following small series and everyone to see the MySQL Take the random data slow optimization process.
MySQL often need to obtain random data, for example, to take a random record from the TableName table, the general way of writing is:
Copy Code code as follows:

SELECT * FROM TableName ORDER by RAND () LIMIT 1

But then I looked up the official MySQL manual, and the hint 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 ().
Test to find that this efficiency is very low. 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 are cannot use a column with RAND () of the clause, because order by would evaluate the column multiple time S.
Search Google, the Internet is basically query Max (ID) * RAND () to randomly obtain data.
Copy Code code as follows:

SELECT *
From ' table ' as T1 JOIN (select ROUND (RAND () * (SELECT MAX (ID) from ' table ') as ID) as T2
WHERE t1.id >= t2.id
ORDER BY T1.id ASC LIMIT 5;

But this will produce a continuous 5 records. 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 statements are used by someone on the Join,mysql forum:
Copy Code code as follows:

SELECT *
From ' table '
WHERE ID >= (SELECT FLOOR (MAX (ID) * RAND ()) from ' table '
ORDER by ID LIMIT 1;

I tested it, it takes 0.5 seconds, and the speed is good, but there is still a big gap with the above statement. There is something wrong with the total sleep.
So the statement was rewritten.
Copy Code code as follows:

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, the statement to improve, plus the 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:
Copy Code code as follows:

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 (
# Minimum value + (1 to smallest and maximum difference)
RAND () * (
(select MAX (ID) from ' table ')-(select MIN (ID) from ' table ')
)
+ (SELECT MIN (ID) from ' table ')
) as ID
) as T2
WHERE t1.id >= t2.id
ORDER by t1.id LIMIT 1;

Finally in PHP, the two statements are queried 10 times, the former takes time 0.147433 seconds, the latter takes 0.015130 seconds. It seems that the syntax for join is much higher than the efficiency of using functions directly in a where.
Copy Code code as follows:

SELECT *
From ' table ' as T1 JOIN (select-ROUND (select MAX (ID) from ' table ')-(select MIN (ID) from ' table ')) + (select M In (ID) from ' table ') as ID) as T2
WHERE t1.id >= t2.id
ORDER by T1.id LIMIT 10;

This is my own choice, from the previous 5 seconds to the present 0.0003 seconds time does not have to find 10 records.
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.