久久久久久久视色,久久电影免费精品,中文亚洲欧美乱码在线观看,在线免费播放AV片

<center id="vfaef"><input id="vfaef"><table id="vfaef"></table></input></center>

    <p id="vfaef"><kbd id="vfaef"></kbd></p>

    
    
    <pre id="vfaef"><u id="vfaef"></u></pre>

      <thead id="vfaef"><input id="vfaef"></input></thead>

    1. 站長資訊網(wǎng)
      最全最豐富的資訊網(wǎng)站

      php如何實現(xiàn)百萬級數(shù)據(jù)快速導(dǎo)出CSV

      在php中,可以使用了服務(wù)器的緩存來實現(xiàn)百萬級數(shù)據(jù)快速導(dǎo)出CSV,今天就帶大家學(xué)習(xí)一下快速導(dǎo)出csv的方式,有需要的可以參考參考。

      php如何實現(xiàn)百萬級數(shù)據(jù)快速導(dǎo)出CSV

      php實現(xiàn)百萬級數(shù)據(jù)導(dǎo)出這里使用了服務(wù)器的緩存來實現(xiàn)

      實現(xiàn):

      一:建立測試表,并加入測試數(shù)據(jù)

      1:創(chuàng)建測試表

      這里我建了一個test表,字段分別為:id,name,age,email

      2:加入測試數(shù)據(jù)

      (1)首先手動向表中添加若干行數(shù)據(jù)

      然后執(zhí)行如下命令批量添加數(shù)據(jù)

      INSERT INTO test (name,age,email) SELECT name,age,email FROM test;

      多次執(zhí)行上面的命令測試數(shù)據(jù)會呈指數(shù)增加,這樣就可以得到很多測試數(shù)據(jù)

      二:php實現(xiàn)導(dǎo)出百萬級數(shù)據(jù)(這里我的查詢命令使用的時Yii框架自帶的查詢命令)

      //讓程序一直運行 set_time_limit(0); //設(shè)置程序運行內(nèi)存 ini_set('memory_limit', '128M'); //導(dǎo)出文件名 $fileName = '測試導(dǎo)出數(shù)據(jù)'; header('Content-Encoding: UTF-8'); header("Content-type:application/vnd.ms-excel;charset=UTF-8"); header('Content-Disposition: attachment;filename="' . $fileName . '.csv"'); //打開php標(biāo)準(zhǔn)輸出流 $fp = fopen('php://output', 'a'); //添加BOM頭,以UTF8編碼導(dǎo)出CSV文件,如果文件頭未添加BOM頭,打開會出現(xiàn)亂碼。 fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF)); //添加導(dǎo)出標(biāo)題 fputcsv($fp, ['姓名', '歲數(shù)', '郵箱']); $nums = 10000; //每次導(dǎo)出數(shù)量 $count = Test::find()->count(); $step = ceil($count/$nums);//循環(huán)次數(shù) for($i = 0; $i < $step; $i++) {     $result = Test::find()         ->select(['name', 'age', 'email'])         ->limit($nums)         ->offset($i * $nums)         ->asArray()         ->all();     foreach ($result as $item) {         fputcsv($fp, $item);     }     //每1萬條數(shù)據(jù)就刷新緩沖區(qū)     ob_flush();     flush(); } exit;

      如上測試后可以發(fā)現(xiàn)導(dǎo)出一百萬左右數(shù)據(jù)只需十幾秒左右時間,效率還算是不錯的

      贊(0)
      分享到: 更多 (0)
      網(wǎng)站地圖   滬ICP備18035694號-2    滬公網(wǎng)安備31011702889846號