亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

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

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

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

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

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

    實(shí)現(xiàn):

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

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

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

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

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

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

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

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

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

    //讓程序一直運(yùn)行 set_time_limit(0); //設(shè)置程序運(yùn)行內(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頭,打開會(huì)出現(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ù)只需十幾秒左右時(shí)間,效率還算是不錯(cuò)的

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