About the solution to PHP randomly recording mysql rand() causing CPU 100%

不言
Release: 2023-04-02 09:54:02
Original
1447 people have browsed it

This article mainly introduces the solution to php randomly recording mysql rand() causing CPU 100%. It has a certain reference value. Now I share it with you. Friends in need can refer to it

mysql The database has tens of thousands of data, and rand() is used to extract 10 random records, causing the server CPU usage to remain high until it crashes~

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

$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";
Copy after login

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 almost as small as It is 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 is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website!

Related recommendations:

How to solve the problem of ThinkPHP implementing multiple database connections

ThinkPHP implements ajax imitation official website search function

The above is the detailed content of About the solution to PHP randomly recording mysql rand() causing CPU 100%. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!