Home > Backend Development > PHP Tutorial > PHP randomly records mysql rand() solution causing CPU 100%_PHP tutorial

PHP randomly records mysql rand() solution causing CPU 100%_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 15:37:32
Original
783 people have browsed it

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

Copy 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. It is to generate a set of random IDs and then retrieve the records corresponding to this set of IDs. After this optimization, the page opening speed It is obviously much faster, and the CPU usage is so small that it is almost 0. Haha~ By the way, I also used this idea to optimize the sqlserver site!
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! ! !

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321896.htmlTechArticleBaidu reviewed some information, combined with some of its own experience, and adopted the following solution: Copy the code as follows: $ idlist=''; for($i=1;$i=20;$i++){ if($i==1){ $idlist=mt_rand(3,...
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template