[MyBatis]再次向MySql一张表插入一千万条数据 批量插入 用时5m24s
本例代码下载:https://files.cnblogs.com/files/xiandedanteng/InsertMillionComparison20191012.rar
环境依然和原来一样。
代码稍改了改:
package com.hy.action;
import java.io.Reader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.log4j.Logger;
import com.hy.entity.Employee;
import com.hy.mapper.EmpMapper;
public class BatchInsert1001 {
private static Logger logger = Logger.getLogger(SelectById.class);
public static void main(String[] args) throws Exception{
long startTime = System.currentTimeMillis();
Reader reader=Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory ssf=new SqlSessionFactoryBuilder().build(reader);
reader.close();
SqlSession session=ssf.openSession();
try {
EmpMapper mapper=session.getMapper(EmpMapper.class);
String ctime="2017-11-01 00:00:01";
int index=0;
for(int i=0;i<10000;i++) {
List<Employee> emps=new ArrayList<Employee>();
for(int j=0;j<1000;j++) {
index++;
Employee emp=new Employee("E"+String.valueOf(index),index % 100,ctime);
emps.add(emp);
ctime=timePastOneSecond(ctime);
}
int changed=mapper.batchInsert(emps);
session.commit();
System.out.println("#"+i+" changed="+changed);
}
}catch(Exception ex) {
session.rollback();
logger.error(ex);
}finally {
session.close();
long endTime = System.currentTimeMillis();
logger.info("Time elapsed:" + toDhmsStyle((endTime - startTime)/1000) + ".");
}
}
public static String timePastOneSecond(String otime) {
try {
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date dt=sdf.parse(otime);
Calendar newTime = Calendar.getInstance();
newTime.setTime(dt);
newTime.add(Calendar.SECOND,1);
Date dt1=newTime.getTime();
String retval = sdf.format(dt1);
return retval;
}
catch(Exception ex) {
ex.printStackTrace();
return null;
}
}
// format seconds to day hour minute seconds style
// Example 5000s will be formatted to 1h23m20s
public static String toDhmsStyle(long allSeconds) {
String DateTimes = null;
long days = allSeconds / (60 * 60 * 24);
long hours = (allSeconds % (60 * 60 * 24)) / (60 * 60);
long minutes = (allSeconds % (60 * 60)) / 60;
long seconds = allSeconds % 60;
if (days > 0) {
DateTimes = days + "d" + hours + "h" + minutes + "m" + seconds + "s";
} else if (hours > 0) {
DateTimes = hours + "h" + minutes + "m" + seconds + "s";
} else if (minutes > 0) {
DateTimes = minutes + "m" + seconds + "s";
} else {
DateTimes = seconds + "s";
}
return DateTimes;
}
}
这把运行时间是5分24秒:
#9988 changed=1000 #9989 changed=1000 #9990 changed=1000 #9991 changed=1000 #9992 changed=1000 #9993 changed=1000 #9994 changed=1000 #9995 changed=1000 #9996 changed=1000 #9997 changed=1000 #9998 changed=1000 #9999 changed=1000 INFO [main] - Time elapsed:5m24s.
数据库结果都正常:


接下来可以做个实验与MyBatis单条插对比一下。
[MyBatis]再次向MySql一张表插入一千万条数据 批量插入 用时5m24s的更多相关文章
- [MyBatis]五分钟向MySql数据库插入一千万条数据 批量插入 用时5分左右
本例代码下载:https://files.cnblogs.com/files/xiandedanteng/InsertMillionComparison20191012.rar 我的数据库环境是mys ...
- [MyBatis]向MySql数据库插入一千万条数据 批量插入用时6分 之前时隐时现的异常不见了
本例代码下载:https://files.cnblogs.com/files/xiandedanteng/InsertMillionComparison20191012.rar 这次实验的环境仍然和上 ...
- mysql一张表到底能存多少数据?
前言 程序员平时和mysql打交道一定不少,可以说每天都有接触到,但是mysql一张表到底能存多少数据呢?计算根据是什么呢?接下来咱们逐一探讨 知识准备 数据页 在操作系统中,我们知道为了跟磁盘交互, ...
- orcle 如何快速插入百万千万条数据
有时候做实验测试数据用到大量数据时可以用以下方法插入: 方法一:使用xmltable create table bqh8 as select rownum as id from xmltable('1 ...
- mysql 导出每张表中的100条数据..............
windows下配好MYSQL 环境变量,cmd 然后: mysqldump -uroot -p123 [数据库名]--where "1=1 limit 100" --lock-a ...
- java之5分钟插入千万条数据
虽说不一定5分钟就插入完毕,因为取决去所插入的字段,如果字段过多会稍微慢点,但不至于太慢.10分钟内基本能看到结果. 之前我尝试用多线程来实现数据插入(百万条数据),半个多小时才二十多万条数据. 线程 ...
- MYSQL单表可以存储多少条数据???
MYSQL单表可以存储多少条数据??? 单表存储四千万条数据,说MySQL不行的自己打脸吧. 多说一句话,对于爬虫来说,任何数据库,仅仅是存储数据的地方,最关心的是 能否存储数据和存储多少数据以及存储 ...
- mysql自定义函数并在存储过程中调用,生成一千万条数据
mysql 自定义函数,生成 n 个字符长度的随机字符串 -- sql function delimiter $$ create function rand_str(n int) returns VA ...
- 使用事务操作SQLite数据批量插入,提高数据批量写入速度,源码讲解
SQLite数据库作为一般单机版软件的数据库,是非常优秀的,我目前单机版的软件产品线基本上全部替换Access作为优选的数据库了,在开发过程中,有时候需要批量写入数据的情况,发现传统的插入数据模式非常 ...
随机推荐
- sql 创建新表时的完成格式
1 create table [dbo].[Customer] ( CustomerID int identity(1,1) not null, [Name] [nvarchar](50) null, ...
- STM32工程模版
STM32工程模版,看过来 ST库源码去官方下载 创建工程目录 doc:存放说明文档 lib:存放库文件 listing:存放编译产生的中间文件 output:存放生成的文件 project:存放工程 ...
- PHP 求两个日期之间相差的天数、月数
<?php /** * 求两个日期之间相差的天数 * (针对1970年1月1日之后,求之前可以采用泰勒公式) * @param string $day1 * @param string $day ...
- MySQL备份--xtrabackup与mysqldump工具使用
MySQL备份----xtrabackup与mysqldump工具的使用 一.Xtrabackup8.0: 一个用于MySQL数据库物理热备的备份工具,支持MySQL.Percona server和M ...
- 命令行工具--LLDP
目录 命令行工具--LLDP 一.场景引入 二.什么是LLDP? 三.在CentOS上安装LLDP 四.命令详解 五.脚本 命令行工具--LLDP 一.场景引入 有的时候,我们需要知道服务器上联交换机 ...
- Redis分布式缓存安装和使用
独立缓存服务器: LinuxCentOS Redis版本: 3.0 下面我们针对于Redis安装做下详细的记录: 编译和安装所需的包: #yum install gcc tcl创建安装目录:贵州中医肝 ...
- kubernetes之download api
download api作用: 可以通过环境变量或Volume挂载将pod信息注入到容器内部 apiVersion: apps/v1 kind: Deployment metadata: name: ...
- CSS基础学习 18.CSS多列
四种常见的浏览器内核:
- ubuntu nginx 启动多个Django项目
1.将 /etc/nginx/sites-enabled/ 目录下的nginx默认配置文件default,重命名,例如:default1 2.给每个Django项目添加nginx.conf配置文件,建 ...
- Maven简介、安装、配置
1.Maven是什么? Maven是一种跨平台的项目管理工具,是一个开源的项目,主要服务于基于java平台的项目构建.依赖管理和项目信息管理.Maven还提供了中央仓库,能帮助我们自动的下载构件.Ma ...