Solution for CPU 100% caused by mysqlrand () randomly recorded by php
Source: Internet
Author: User
The mysql database has tens of thousands of data records, and uses rand () to extract 10 random records. as a result, the server's cpu usage remains high until it crashes ~ Baidu consulted some materials and combined with some of its own experience, adopted the following solutions:
The code is as follows:
$ Idlist = '';
For ($ I = 1; $ I <= 20; $ I ++ ){
If ($ I = 1 ){
$ Idlist = mt_rand (3,25216 );
}
Else {
$ Idlist = $ idlist. ','. mt_rand (3,25216 );
}
}
$ Query = "select * from table where id in ($ idlist) LIMIT 0, 10 ";
The principle is actually very simple. it is to generate a group of random IDs and then retrieve the records corresponding to this group of IDS. after such optimization, the page opening speed is much faster, the CPU usage is almost zero ~ By the way, I optimized the SQL Server site!
Speed comparison:
It takes more than 400 milliseconds to use rand () to retrieve 10 random records, and the above method only takes about 0.6 milliseconds. The difference is too big !!!
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.