Solution to CPU caused by mysqlrand () randomly recorded by php _ PHP Tutorial

Source: Internet
Author: User
Php randomly records mysqlrand () causes a CPU usage. Baidu consulted some materials and combined with some of its own experience, adopted the following solution: Copy the code as follows: $ idlist; for ($ i1; $ i20; $ I ++) {if ($ i1) {$ idlistmt_rand (3. Baidu consulted some materials and combined with some of its own experiences, adopted the following solutions:

The 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 !!!

Pipeline code: $ idlist = ''; for ($ I = 1; $ I = 20; $ I ++) {if ($ I = 1) {$ idlist = mt_rand (3 ,...

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: info-contact@alibabacloud.com 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.