Home > Backend Development > PHP Tutorial > cpu 100 php random recording mysql rand causes CPU 100% solution

cpu 100 php random recording mysql rand causes CPU 100% solution

WBOY
Release: 2016-07-29 08:42:51
Original
1234 people have browsed it

Baidu consulted some information, combined with some of its own experience, and adopted the following solution:

Copy the code 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, that is, generate a set of random IDs, and then retrieve the records corresponding to this set of IDs , after such optimization, the page opening speed is obviously much faster, and the CPU usage is also small to almost 0. Haha~ By the way, I also optimized the sqlserver site using this idea!
Speed ​​comparison:
It takes more than 400 milliseconds to use rand() to get 10 random records, but it only takes about 0.6 milliseconds to use the above method. The difference is huge! ! !

The above introduces the solution to CPU 100% caused by cpu 100 php randomly recording mysql rand, including the content of cpu 100. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template