Solution to CPU 100% caused by PHP logging MySQL rand () at random

Source: Internet
Author: User

Baidu consulted some materials and combined with some of its own experience, adopted the following solutions: CopyCodeThe 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 group of random IDs and then retrieve the records corresponding to this group of IDs. After such optimization, the page opening speed is much faster, the CPU usage is almost zero ~ By the way, I optimized the SQL server site!
Speed comparison:
It takes more than 400 milliseconds to use rand () to retrieve 10 random records, and the above method only takes about 0.6 milliseconds. The difference is too big !!!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.