PHP program randomly records mysql rand(), causing CPU 100% solution

WBOY
Release: 2016-07-25 09:03:39
Original
917 people have browsed it
  1. $idlist='';
  2. for($i=1;$i<=20;$i++){
  3. if($i==1){
  4. $idlist=mt_rand( 3,25216);
  5. }
  6. else{
  7. $idlist=$idlist.','.mt_rand(3,25216);
  8. }
  9. }
  10. $query="select * from table where id in ($idlist) LIMIT 0 ,10";
  11. ?>
Copy code

Principle analysis: 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 significantly faster, and the CPU usage is reduced to almost 0. By the way, this idea is used to optimize the site in the sql server database application. Speed ​​comparison: Using rand() to retrieve 10 random records takes more than 400 milliseconds, while using the above method only takes about 0.6 milliseconds. The efficiency is not improved a little.

Articles you may be interested in: php code to get CPU usage Use proc/loadavg in php to monitor the average load of the CPU php code to record server load, memory, cpu status Example of php page caching (reducing the burden on cpu and mysql) An example of php code to obtain cpu and memory usage php implementation code to obtain Linux server CPU, memory, hard disk usage php code to obtain the computer’s unique identification information (cpu, network card, MAC address) The relationship between PHP-CGI process CPU 100% and file_get_contents function php gets CPU usage information



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