溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

php導(dǎo)致內(nèi)存溢出

發(fā)布時(shí)間:2020-03-02 16:57:39 來源:網(wǎng)絡(luò) 閱讀:465 作者:juggles 欄目:web開發(fā)

在執(zhí)行一個(gè)導(dǎo)出csv腳步時(shí),當(dāng)要導(dǎo)出的數(shù)據(jù)超過3w多條時(shí),就會(huì)報(bào)錯(cuò),如下:

Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes)

php存儲3w多條數(shù)據(jù)這個(gè)數(shù)組占用內(nèi)存超過20M

解決方法:分批取數(shù)據(jù),分批處理數(shù)據(jù)
問題點(diǎn):一次取多少數(shù)據(jù)最合適
一次取1w條,減少數(shù)據(jù)庫IO操作次數(shù),但php數(shù)組就好較大
一次取1k條,增加了數(shù)據(jù)庫IO操作次數(shù),php數(shù)組很小

php導(dǎo)致內(nèi)存溢出

數(shù)據(jù)一共41000條
因?yàn)槭茄h(huán)查詢數(shù)據(jù),只能返回空,所以會(huì)有一次無效的IO查詢

每次請求次數(shù) 響應(yīng)時(shí)間 數(shù)組庫IO次數(shù)
5000 11.34 10
1000 8.02 6
2000 7.07 4
3000 5.12 3
4000 4.93 3

綜合考慮,覺得使用每次請求1w條數(shù)據(jù)

代碼簡單記錄下

$filename = '測試';
$head  = array('申請時(shí)間', '省', '市', '超市品牌');
$field = array('created_time', 'pro_name', 'city_name', 'brand_name');
$csv   = ExportCsvService::getInstance(implode(",", $head), $field, $filename);

$param['limit'] = C('SQL_LIMIT');
$i              = 0;
while (true) {
        $param['current'] = $i + 1;
        $info             = $rainbowservice->get_list($param)['data']['list'];

        if (empty($info)) {
                $csv->end();
                break;
        } else {
                $csv->sendDate($info);
        }
        unset($info);
        $i++;
}

csv操作類


class ExportCsvService
{

    private $content;
    private $filename;
    private $field;
    private static $instance;

    private function __construct($head,$field,$filename)
    {
        $this->field = $field;
        $this->filename = $filename;
        $this->content  = iconv('UTF-8', 'GBK',$head) . PHP_EOL;
    }

    /**
     * 實(shí)例化服務(wù)類
     * @param $head
     * @param $field
     * @param $filename
     * @return ExportCsvService
     */
    public static function getInstance($head,$field,$filename) {
        if (!self::$instance instanceof self) {
            self::$instance = new self($head,$field,$filename);
        }
        return self::$instance;
    }

    /**
     * 拼接要導(dǎo)出的數(shù)據(jù)
     * @param $data
     */
    public function sendDate($data) {

        foreach ($data as $key => $value) {
            $temp = [];
            foreach ($this->field as $k) {
                $temp[] = iconv('UTF-8', 'GBK', $value[$k]);
            }

            $this->content .= implode(",", $temp) . PHP_EOL; //用英文逗號分開
            unset($temp);
        }
    }

    /**
     * 輸出數(shù)據(jù)
     */
    public function end() {
        header("Content-type:text/csv;charset=GBK");
        header("Content-Disposition:attachment;filename=" . $this->filename);
        header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
        header('Expires:0');
        header('Pragma:public');
        echo $this->content;
    }

}
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI