博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Mysql数据库实践操作之————批量插入数据(100万级别的数据)
阅读量:6713 次
发布时间:2019-06-25

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

第一种方法:使用insert into 插入

从Redis每次获取100条数据,根据条件去插入到Mysql数据库中:

条件:

如果当前队列中的值大于1000条,则会自动的条用该方法,该方法每次获取从队列的头部每次获取100掉数据插入到Mysql数据库中,同时以当前队列的长度为插入条件。

1000为原始数据,从队列头获取100条,插入到Mysql数据,同时删除已经插入的数据,再通过队列的长度判断是否继续插入,直到循环不满足条件为止。

[1]获取头100条数据:$redis->lRange($liveKey,0,99)

[2]删除头100条数据:$redis->lTrim($liveKey, 100, -1);

[1]获取当前队列长度:$redis->lLen($liveKey);

public function redisSaveMysqlAction()    {        $liveKey = $this->request->getQuery('liveKey');        if(empty($liveKey)){            $result = array("errcode" => 500, "errmsg" => "this parameter is empty!");            return $this->toJson($result);        }        $redis = new \Redis();        $redis->connect('1.1.2.16', '6379');        $redisInfo = $redis->lRange($liveKey,0,99);        $dataLength = $redis->lLen($liveKey);        while($dataLength > 200) {            try {                $this->db->begin();                foreach ($redisInfo as $action) {                    $sql = "INSERT INTO livecomment (liveId,username,createTime,userId,content) VALUES (?, ? ,?,? ,?)";                    $this->db->execute($sql, array(                        json_decode($action,true)['roomId'],                        json_decode($action,true)['userName'],                        json_decode($action,true)['createTime'],                        json_decode($action,true)['userId'],                        json_decode($action,true)['content'],                    ));                }                $redis->set('message_insert_success', '1');                $redis->lTrim($liveKey, 100, -1);                $redisInfo = $redis->lRange($liveKey,0,99); // 这句也要重新的获取,不然就会插入重复的数据,也就是获取删除后的数据                $dataLength = $redis->lLen($liveKey); //注意这句一定要加上的,做为下一次的判断标准,当插入完后和删除后,重新获取列表的长度,作为条件依据                $redis->set('dataLength_backenk', $dataLength);                $this->db->commit();            } catch (\Exception $e) {                $redis->set('message_insert_fail', '0');                $this->db->rollback();            }        }        $redis->set('log'.$liveKey,$redis->incr('request_counts'));        $result = array("errcode" => 200, "errmsg" => "Data Insert into Success!",'data'=>'dataLength:'.$dataLength.'liveKey:'.$liveKey);        return $this->toJson($result);

第二种方法:使用优化SQL语句:将SQL语句进行拼接,使用 insert into table () values  (),(),(),()然后再一次性插入,如果字符串太长,则需要配置下MYSQL,在mysql 命令行中运行 :set global max_allowed_packet =  2*1024*1024*10;

拼接后的字符串:

'insert into twenty_million (value) values('50'),('50'),('50'),('50'),('50'),('50'),('50'),('50'),('50'),('50')'

实际案例:

/**     * 获取Redis数据批量的保存到Redis中去解析Redis数据的json格式     */    public function RedisSaveToMysqlJsonAction()    {        $redis = RedisInstance::getInstance();        $redis->select(1);        $redisInfo = $redis->lRange('message01',0,9999);        $dataLength = $redis->lLen('message01');        $redis->set('dataLength_front',$dataLength);        $t1=microtime(true);        while($dataLength > 20000) {            try {                $this->db->begin();                $sql = "INSERT INTO stream_name (name,createTime,userId,content) VALUES";                foreach ($redisInfo as $action) {                    $sql .= "('" . json_decode($action, true)['userName'] . "',                    '" . json_decode($action, true)['createTime'] . "',                    '" . json_decode($action, true)['userId'] . "',                    '" . json_decode($action, true)['content'] . "'),";                }                $sql = rtrim($sql, ',');                $this->db->execute($sql);                $redis->lTrim('message01', 10000, -1);                $redisInfo = $redis->lRange('message01',0,9999);                $dataLength = $redis->lLen('message01');                $this->db->commit();            } catch (\Exception $e) {                $redis->set('message_catch', json_encode($e));                $this->db->rollback();            }        }        echo 'ENDTIME:'.(microtime(true)-$t1)."
"; echo 'success'; die; }

输出结果为:

ENDTIME:3.0146479606628(s)success

 

转载地址:http://iaslo.baihongyu.com/

你可能感兴趣的文章
Java Thread Dumps 日志分析
查看>>
10g ASM lost disk log
查看>>
php---如何使用php链接mongodb(windows)
查看>>
ubuntu10.10---Installing Django with Apache and...
查看>>
TypeScript基础入门 - 类 - 抽象类
查看>>
Hibernate学习2--理解Hibernate的的核心接口,实例状态及缓存相关
查看>>
memcache
查看>>
windows7 python3和python2 环境安装
查看>>
FFmpeg发送流媒体的命令(UDP,RTP,RTMP)
查看>>
2012,2013年总结:在视音频技术道路上摸索
查看>>
Hibernate 单向 多对一
查看>>
OneAPM Cloud Test——系统性能监控神器
查看>>
如何助力企业 APP 在竞争中占据先机?
查看>>
new与malloc的几点区别
查看>>
使用ProgressBar实现进度条
查看>>
简单组合java.util.Map<K,V>实现Map<K,P,V>
查看>>
mysql 实用
查看>>
java相关的jar包中的实用方法总结
查看>>
ym——Android仿QQ5.0侧滑菜单ResideMenu源码分析
查看>>
虚拟机中centos的安装
查看>>