php程式隨機記錄mysql rand()造成CPU 100%的解決方案

來源:互聯網
上載者:User
  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. ?>
複製代碼

原理分析:產生一組隨機ID,然後檢索這一組ID對應的記錄。經過這樣最佳化,頁面開啟速度明顯快了很多,CPU佔用率也小到幾乎為0。順便用這個思路把sql server資料庫應用中的網站也最佳化了一番。速度比較:用rand()取10條隨機記錄要400多毫秒,而用上面的方法則只要0.6毫秒左右,效率提高了不是一點點啊。

您可能感興趣的文章:php擷取CPU使用方式的代碼php中使用proc/loadavg監控CPU的平均負載php記錄伺服器負載、記憶體、cpu狀態的代碼php頁面緩衝的例子(減經cpu與mysql負擔)php擷取cpu與記憶體使用量情況的代碼一例php擷取linux伺服器CPU、記憶體、硬碟使用率的實現代碼php擷取電腦唯一標識資訊(cpu,網卡,MAC地址)的代碼PHP-CGI 進程 CPU 100% 與 file_get_contents 函數的關係php擷取CPU使用資訊

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    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.