This article mainly introduces the PHP random record MySQL rand () caused by the CPU 100% solution, has a certain reference value, now share to everyone, the need for friends can refer to
MySQL database has 10 tens of thousands of data, using rand () to extract random 10 records, resulting in high server CPU consumption until the crash ~
Baidu consulted some of the information, combined with some of their own experience, the following solutions:
$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, is to produce a set of random ID, and then retrieve the set of IDs corresponding records, after such optimization, the page open speed significantly faster, CPU occupancy rate is almost 0, hehe ~ the way to use this idea of SQL Server site also optimized a bit!
Speed comparison:
Use Rand () to take 10 random records to 400 milliseconds, and the above method as long as 0.6 milliseconds, the difference is too big!!!
The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!