Home > Backend Development > PHP Tutorial > php 随机记录mysql rand()造成CPU 100%的解决办法_php技巧

php 随机记录mysql rand()造成CPU 100%的解决办法_php技巧

WBOY
Release: 2016-05-17 09:25:15
Original
1023 people have browsed it

百度查阅了一些资料,再结合自己的一些经验,采用以下解决办法:

复制代码 代码如下:

$idlist='';
for($i=1;$iif($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";

原理其实很简单,就是产生一组随机ID,然后检索这一组ID对应的记录,经过这样优化,页面打开速度明显快了很多,CPU占用率也小到几乎为0,呵呵~顺便用这个思路把sqlserver站点也优化了一下!
速度比较:
用rand()取10条随机记录要400多毫秒,而用上面的方法只要0.6毫秒左右,差别太大了!!!
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