使用thinkphp框架
先生成包含所有数据的数组,再使用 addAll() 方法,插入1万条数据仅需3秒钟。
//给所有用户推送系统消息 public function pushSystemMessage(){ if(IS_AJAX){ $platform = I('post.platform');//适用平台 1-wap端,2-PC端,3-APP端 $platfrom = [1,2,3] $pushUser = I('post.pushUser'); $id = I('post.id'); //全部用户 if($pushUser == 'all'){ $allUser = M('user')->order('user_id desc')->field('user_id')->select(); $time = time(); foreach ($platform as $pkey => $pvalue) { $platFormData = array( 'platform' => $pvalue, ); $messageDatas = []; foreach ($allUser as $key => $value) { $messageData = array( 'user_id' => $value['user_id'], 'mc_id' => $id, 'atime' => $time, ); $messageDatas[] = array_merge($platFormData,$messageData); } $lastId = $this->messageCenterUser->addAll($message_Datas);//这个是关键 } //部分用户,后期扩展 }else{ } if(!is_numeric($lastId)){ echo json_encode(['info'=>'系统消息推送失败!','status'=>'0']); }else{ //推送成功,把对应的系统消息变成已推送 $data = ['is_push'=>1]; M('message_c')->where('id='.$id)->save($data); echo json_encode(['info'=>'推送系统消息成功!','status'=>'1']); } }else{ $id = I('get.id'); if($id){ $systemMessage = M('message_c')->where('id='.$id)->find(); $this->assign('systemM',$systemMessage); } $this->display('pushSystemMessage'); } }