**批量添加数据**
`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();}