php实现百万级数据快速导出CSV

//让程序一直运行
set_time_limit(0);
//设置程序运行内存
ini_set('memory_limit', '128M');
//导出文件名
$fileName = '测试导出数据';
header('Content-Encoding: UTF-8');
header("Content-type:application/vnd.ms-excel;charset=UTF-8");
header('Content-Disposition: attachment;filename="' . $fileName . '.csv"');
//打开php标准输出流
$fp = fopen('php://output', 'a');
//添加BOM头,以UTF8编码导出CSV文件,如果文件头未添加BOM头,打开会出现乱码。
fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));
//添加导出标题
fputcsv($fp, ['姓名', '岁数', '邮箱']);
$nums = 10000; //每次导出数量
$count = Test::find()->count();
$step = ceil($count/$nums);//循环次数
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万条数据就刷新缓冲区
    ob_flush();
    flush();
}
exit;

http://mirrors.cloud.aliyuncs.com/centos/6/os/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 22

http://mirrors.cloud.aliyuncs.com/centos/6/os/x86_64/repodata/repomd.xml: [Errno 14] PYCURL ERROR 6 – Couldn’t resolve host ‘mirrors.cloud.aliyuncs.com’

是因为已经找不到这个版本的文件了,自然就报错了;将配置文件改为centos7就能找到了

cd /etc/yum.repos.d
vi CentOS-Base.repo
:%s/$releasever/7/g  #将文件中$releasever全部改成7
yum clean all 
yum repolist