关于 MyBatis MyBatis-Spring Jdbc 批量插入的各种比较分析
因为目前SME项目中编写了一套蜘蛛爬虫程序,所以导致插入数据库的数据量剧增。就项目中使用到的3种DB插入方式进行了一个Demo分析:
具体代码如下:
1: MyBatis 开启Batch方式,最普通的带自动事务的插入:
SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH, true);
try { Date dt1 = new Date(); SemAccountMapper dao = session.getMapper(SemAccountMapper.class);
dao.addSemAccountBatch(accounts);
//session.commit();
//session.clearCache();
Date dt2 = new Date(); return (dt2.getTime() - dt1.getTime()) / 1000.0; } catch (Exception e) {
session.rollback();
throw e;
}
1.1 开启Batch,但不使用自动事务
SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
try { Date dt1 = new Date(); SemAccountMapper dao = session.getMapper(SemAccountMapper.class);
dao.addSemAccountBatch(accounts);
session.commit();
session.clearCache();
Date dt2 = new Date(); return (dt2.getTime() - dt1.getTime()) / 1000.0; }
2: MyBatis_spring 普通的插入
SqlSession session = sqlSessionFactory.openSession(ExecutorType.BATCH, true);
try {
Date dt1 = new Date();
SemAccountMapper dao = session.getMapper(SemAccountMapper.class);
dao.addSemAccountBatch(accounts);
session.commit();
session.clearCache();
Date dt2 = new Date();
return (dt2.getTime() - dt1.getTime()) / 1000.0;
}
3:Jdbc 批量操作,设置普通连接字符串
public static final String DBURL = "jdbc:mysql://192.168.21.225:3306/sem"; Connection con = null; // 表示数据库的连接对象
Class.forName(DBDRIVER); // 1、使用CLASS 类加载驱动程序
con = DriverManager.getConnection(DBURL, DBUSER, DBPASS); // 2、连接数据库 try {
con.setAutoCommit(false);
String sql = "Insert into" + " sem_account(ClientId,Name,SEAccountName,SEPassword,SEStatus,Status,CreatorId,CreatedTime,LastChanged) " + " Values "
+ " (?,?,?,?,?,?,?,?,?) ";
PreparedStatement prest = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY); for (int i = 0; i < length; i++) {
prest.setInt(1, 1);
prest.setString(2, i + "");
prest.setString(3, i + "");
prest.setString(4, i + "");
prest.setInt(5, 1);
prest.setInt(6, 1);
prest.setInt(7, 1);
prest.setString(8, DateUtil.getCurrentDate());
prest.setString(9, DateUtil.getCurrentDate());
prest.addBatch();
} Date dt1 = new Date(); prest.executeBatch();
con.commit(); Date dt2 = new Date(); return (dt2.getTime() - dt1.getTime()) / 1000.0;
}
3.1 设置批量连接字符串 rewriteBatchedStatements=true,与上面不同的只有连接字符串不同
public static final String DBURL = "jdbc:mysql://192.168.21.225:3306/sem?rewriteBatchedStatements=true";
以上所有方法,在测试程序中,都执行2次,防止有连接池缓存影响速度上的判断,插入数据库数据位10w条数据,得到以下分析结果:
插入数据库方式 | 第一次插入10w条时间(s) | 第二次插入10w条时间(s) |
MyBatis 开启Batch 自动事务提交 | 42.703 | 35.747 |
MyBatis 开启Batch 关闭自动事务,自提交 | 38.875 | 40.285 |
MyBatis-Spring 普通提交 | 37.199 | 36.607 |
Jdbc 批量操作,普通连接字符串 | 98.589 | 97.973 |
Jdbc 批量操作,设置批量操作字符串 | 3.311 | 3.244 |
从以上操作来看,其中Mybatis自动事务,和Mybatis-Spring 的方法所用时间基本一致,
而最快的操作在 Jdbc 带上批量操作参数以后的速度 3.244s。
附带InsertBatch的Mapper文件
<insert id="addSemAccountBatch" parameterType="SemAccount">
Insert into sem_account
(ClientId,Name,SyncId,SyncTime,SEId,SESyncTime,SEAccountName,SEPassword,SEApiToken,SEStatus,Cost,BudgetPerDay,Balance,Payment,ExcludeIps,RegionTargets,OpenDomains,BudgetOfflineTime,Remark,WarningRemainingDays,WarningRemainingCost,Status,CreatorId,CreatedTime,LastChanged,SearchEngineType,SearchEngineConstId)
Values
<foreach collection="list" item="item" index="index"
separator=",">
(#{item.clientId},#{item.name},#{item.syncId},#{item.syncTime},#{item.sEId},#{item.sESyncTime},#{item.sEAccountName},#{item.sEPassword},#{item.sEApiToken},#{item.sEStatus},#{item.cost},#{item.budgetPerDay},#{item.balance},#{item.payment},#{item.excludeIps},#{item.regionTargets},#{item.openDomains},#{item.budgetOfflineTime},#{item.remark},#{item.warningRemainingDays},#{item.warningRemainingCost},#{item.status},#{item.creatorId},#{item.createdTime},#{item.lastChanged},#{item.searchEngineType},#{item.searchEngineConstId})
</foreach>
</insert>
关于 MyBatis MyBatis-Spring Jdbc 批量插入的各种比较分析的更多相关文章
- spring jdbc批量插入
http://blog.csdn.net/fyqcdbdx/article/details/7366439
- Mybatis与JDBC批量插入MySQL数据库性能测试及解决方案
转自http://www.cnblogs.com/fnz0/p/5713102.html 不知道自己什么时候才有这种钻研精神- -. 1 背景 系统中需要批量生成单据数据到数据库表,所以采用 ...
- 三种JDBC批量插入编程方法的比较
JDBC批量插入主要用于数据导入和日志记录因为日志一般都是先写在文件下的等. 我用Mysql 5.1.5的JDBC driver 分别对三种比较常用的方法做了测试 方法一,使用PreparedStat ...
- jdbc批量插入
分享牛,分享牛原创.有这样一个需求,文本文件中的数据批量的插入mysql,怎么用jdbc方式批量插入呢? jdbc默认提供了批量插入的方法,可能用一次就忘记了,这里做笔记记录一下jdbc批量插入吧. ...
- JDBC批量插入数据优化,使用addBatch和executeBatch
JDBC批量插入数据优化,使用addBatch和executeBatch SQL的批量插入的问题,如果来个for循环,执行上万次,肯定会很慢,那么,如何去优化呢? 解决方案:用 preparedSta ...
- 【MyBatis】几种批量插入效率的比较
批处理数据主要有三种方式: 反复执行单条插入语句 foreach 拼接 sql 批处理 一.前期准备 基于Spring Boot + Mysql,同时为了省略get/set,使用了lombok,详见p ...
- MyBatis向数据库中批量插入数据
Foreach标签 foreach: collection:指定要遍历的集合; 表示传入过来的参数的数据类型.该参数为必选.要做 foreach 的对象,作为入参时,List 对象默认用 list 代 ...
- mybatis 注解的方式批量插入,更新数据
一,当向数据表中插入一条数据时,一般先检查该数据是否已经存在,如果存在更新,不存在则新增 使用关键字 ON DUPLICATE KEY UPDATE zk_device_id为主键 model ...
- mybatis使用foreach进行批量插入和删除操作
一.批量插入 1.mapper层 int insertBatchRoleUser(@Param("lists") List<RoleUser> lists);//@Pa ...
随机推荐
- 推荐书目 - C++学习资料
前言 在本文的前半部分我我会谈谈 我看过的书,和我个人的一些理解 ,并且会提供 C++标准委员会相关链接 和 C++第三方轮子/库总结 .本文的后半部分翻译了来自 The Definitive C++ ...
- iOS8毛玻璃效果
UIBlurEffect*blueEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]; UIVisualEffectView* ...
- android 后台运行
改写返回键事件监听,使得back键功能类似home键,让Acitivty退至后台时不被系统销毁,代码如下: public boolean onKeyDown(int keyCode, KeyEvent ...
- hadoop生态圈安装详解(hadoop+zookeeper+hbase+pig+hive)
-------------------------------------------------------------------* 目录 * I hadoop分布式安装 * II zoo ...
- DRBD脑裂解决方法
1.查看主服务器 [root@master ~]# /etc/init.d/drbd status drbd driver loaded OK; device status: version: (ap ...
- Scala基础入门-3
学习Scala——映射和元组 映射和和元组,也就是Maps和Tuples.Map这东西应该都挺明白的,就是键值对的集合.而元组,tuple,这东西并不是每个语言都有(Python中是有的,不过当时学的 ...
- Android 开源控件系列_1
第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...
- 使用Python把Gtest XML测试结果转换为HTML格式
在最近的测试中,使用gtest测试框架对c语言代码进行测试,结果以XML文件来保存,但是测试结果的查阅和分析非常不方便.便想着把xml的结果直接转为HTML文件,方便和Jenkins系统对接显示.因现 ...
- 将warning设为错误
在程序编写过程中,我们有时会因为现实情况将现在无法实现的部分功能设为warning #prama warning ------------------ 为了方便查找warning,或查看某部分的警告, ...
- DenyHosts安装及配置
一.DenyHost简介 DenyHosts是Python语言写的一个程序软件,运行于Linux上预防SSH暴力破解的,它会分析sshd的日志文件(/var/log/secure),当发现重复的攻击时 ...