PHP random record MySQL rand () causes CPU 100% Workaround _php Tutorial

Source: Internet
Author: User
Baidu consulted some of the information, combined with some of their own experience, the following solutions:
Copy CodeThe 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, 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!!!

http://www.bkjia.com/PHPjc/321896.html www.bkjia.com true http://www.bkjia.com/PHPjc/321896.html techarticle Baidu consulted some of the information, combined with some of its own experience, the following solutions: Copy code code as follows: $idlist = "; for ($i =1; $i =20; $i + +) {if ($i ==1) {$idlist =mt_rand (3,...

  • 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: 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.