zoukankan      html  css  js  c++  java
  • php(thinkphp)插入上万条mysql数据最快的方法!

    使用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');
            }
        }
     
  • 相关阅读:
    如何在iTerm2中配置oh my zsh?
    sublime中格式化jsx文件
    ES6 new syntax of Literal
    ES6 new syntax of Rest and Spread Operators
    How to preview html file in our browser at sublime text?
    ES6 new syntax of Default Function Parameters
    ES6 new syntax of Arrow Function
    七牛云2018春招笔试题
    Spring-使用注解开发(十二)
    Spring-声明式事物(十一)
  • 原文地址:https://www.cnblogs.com/Essaycode/p/10160451.html
Copyright © 2011-2022 走看看