博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
laravel 框架下 使用mysql 批量添加数据
阅读量:5344 次
发布时间:2019-06-15

本文共 1432 字,大约阅读时间需要 4 分钟。

**批量添加数据**

 

`class Messages extends ChefModel {//protected $table = 'messages';// 批量添加 性能更好,推荐使用public static function batchAdd($data) {DB::table('messages')->insert($data);}}

  

实测,每次添加5000条数据时,速度最快

public function send($pushLog) {$ftype = $pushLog->ftype;$title = $pushLog->title;$content = $pushLog->content;$description = $pushLog->description;$isAll = $pushLog->is_all;$userIdArray = [];if ($isAll == 1) {$userIdArray = User::pluck("id");} else {$userIdArray = explode(",", $description);}$num = 5000; //分组,每次群发5000个用户,实测5000个速度最快$group = [];$created = Date("Y-m-d H:i:s", time());$updated = Date("Y-m-d H:i:s", time());Log::debug("groupdata start:" . Date("H:i:s", time()));for ($i = 0; $i < count($userIdArray); $i++) {$group[$i / $num][] = ['user_id' => $userIdArray[$i],'title' => $title,'remark' => $content,'ftype' => 3, //系统消息'created_at' => $created,'updated_at' => $updated];}Log::debug("groupdata finish:" . Date("H:i:s", time()));foreach ($group as $key => $data) {Messages::batchAdd($data);if ($isAll == 0) {UserProfile::whereIn("user_id", array_column($data, 'user_id'))->increment("system_count");}}if ($isAll == 1) {UserProfile::increment("system_count");}Log::debug("adddata finish:" . Date("H:i:s", time()));$pushLog->status = 1; // 发送成功$pushLog->success_users = count($userIdArray);$pushLog->updated_at = Date("Y-m-d H:i:s", time());$pushLog->save();}

  

转载于:https://www.cnblogs.com/lianruihong/p/10566875.html

你可能感兴趣的文章
WebSocket 时时双向数据,前后端(聊天室)
查看>>
关于python中带下划线的变量和函数 的意义
查看>>
linux清空日志文件内容 (转)
查看>>
安卓第十三天笔记-服务(Service)
查看>>
Servlet接收JSP参数乱码问题解决办法
查看>>
Ajax : load()
查看>>
MySQL-EXPLAIN执行计划Extra解释
查看>>
Zookeeper概述
查看>>
Zookeeper一致性级别
查看>>
Linux远程登录
查看>>
Linux自己安装redis扩展
查看>>
HDU 1016 Prime Ring Problem(dfs)
查看>>
C#中结构体与字节流互相转换
查看>>
session和xsrf
查看>>
Linux目录结构
查看>>
luoguP3414 SAC#1 - 组合数
查看>>
五一 DAY 4
查看>>
(转)接口测试用例设计(详细干货)
查看>>
【译】SSH隧道:本地和远程端口转发
查看>>
图片点击轮播(三)-----2017-04-05
查看>>