第一种方法:使用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)."<BR/>";
echo 'success';
die;
}

输出结果为:

ENDTIME:3.0146479606628(s)
success

Mysql数据库实践操作之————批量插入数据(100万级别的数据)的更多相关文章

  1. 关于批量插入数据之我见(100万级别的数据,mysql) (转)

    因前段时间去面试,问到如何高效向数据库插入10万条记录,之前没处理过类似问题,也没看过相关资料,结果没答上来,今天就查了些资料,总结出三种方法: 测试数据库为MySQL!!! 方法一: public  ...

  2. 关于批量插入数据之我见(100万级别的数据,mysql)

    因前段时间去面试,问到怎样高效向数据库插入10万条记录,之前没处理过类似问题.也没看过相关资料,结果没答上来,今天就查了些资料.总结出三种方法: 測试数据库为mysql!!! 方法一: public ...

  3. .net使用SqlBulkCopy类操作DataTable批量插入数据库数据,然后分页查询坑

    在使用SqlBulkCopy类操作DataTable批量插入数据,这种操作插入数据的效率很高,就会导致每一条数据在保存的时间基本一样,在我们分页查询添加的数据是,使用数据的添加时间来排序就会出现每页的 ...

  4. mysql三种带事务批量插入

    原文:mysql三种带事务批量插入 c#之mysql三种带事务批量插入 前言 对于像我这样的业务程序员开发一些表单内容是家常便饭的事情,说道表单 我们都避免不了多行内容的提交,多行内容保存,自然要用到 ...

  5. DBA必备:MySQL数据库常用操作和技巧

    DBA必备:MySQL数据库常用操作和技巧 2011-02-25 15:31 kaduo it168 字号:T | T MySQL数据库可以说是DBA们最常见和常用的数据库之一,为了方便大家使用,老M ...

  6. Mysql数据库基础操作

    Mysql数据库基础操作 在mysql数据库中开启使用tab键补全功能 1)修改主配置文件/etc/mysql/my.cnf(mysql和mariadb目录有些不同) vim /etc/mysql/m ...

  7. MySQL 数据库高级操作 (配图)

    MySQL数据库高级操作 1.一键部署mysql 数据库 2.数据表高级操作 3.数据库用户管理 4.数据库用户授权 1.首先一键部署mysql 数据库 : 可以看我之前的博客 https://www ...

  8. zabbix数据库mariadb从服务器迁移到云mysql数据库的操作

    zabbix数据库mariadb从本机迁移到云mysql数据库的操作 1.将zabbix数据库导出,并导入到云数据库中 由于数据库较大,如果直接使用shell会话中断会导致数据库导出或者导入失败,使用 ...

  9. Database学习 - mysql 数据库 表操作

    mysql 数据库 表操作 创建数据表 基本语法格式: 创建数据表: create table 表名( 字段名 datatype 约束, 字段名 datatype 约束, ...... ) 修改表名 ...

随机推荐

  1. Hibernate API申明事务边界

    在Hibernate API中,Session和Transaction接口提供了以下声明事务边界的方法: 声明事务的开始边界: Transaction tx = session.beginTransa ...

  2. Java中Synchronized的用法

    原文:http://blog.csdn.net/luoweifu/article/details/46613015 作者:luoweifu 转载请标名出处 <编程思想之多线程与多进程(1)——以 ...

  3. 在discuz二次开发模板时,diy编辑显示我“抱歉,您没有权限添加此模块

    <div id="diy_vk_portal_slide_top" class="area"><div id="frameCRxR0 ...

  4. html 和 html5(一)(表格 | 列表 | 提交按钮 | 单选 |复选 | 框架 | 脚本 | html字符实体 )

    一.框架 使用iframe来显示目录链接页面 iframe可以显示一个目标链接的页面 目标链接的属性必须使用iframe的属性,如下实例: 实例 <iframe src="demo_i ...

  5. YTU 2987: 调整表中元素顺序(线性表)

    2987: 调整表中元素顺序(线性表) 时间限制: 1 Sec  内存限制: 2 MB 提交: 1  解决: 1 题目描述 若一个线性表L采用顺序存储结构存储,其中所有元素都为整数.设计一个算法,将所 ...

  6. Round Numbers(组合数学)

    Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10484 Accepted: 3831 Descri ...

  7. MVC4 经典增删改查详情demo

    MVC4 经典增删改查详情demo 源码 不解释 Mvc4增删改查详情Demo.7z public ActionResult Detail(int? id)  {    ViewData.Model ...

  8. Infragistics UltraGrid的使用

    OL SDK:http://help.infragistics.com/ 资料参考:http://blog.csdn.net/andy_212/article/details/4019895 http ...

  9. SGU 105 div.3 找规律

    There is sequence 1, 12, 123, 1234, ..., 12345678910, ... . Given first N elements of that sequence. ...

  10. Git笔记 整理2

    补充: 1,如何只克隆git仓库中的一个分支? git clone -b <branch> <remote_repo> eg:  git clone -b vivien_dev ...