MySQL使用Batch批量处理
来源:互联网 发布:手机淘宝怎么定时上架 编辑:程序博客网 时间:2024/05/18 03:06
转自http://blog.itpub.net/29254281/viewspace-1151785/
使用MySQL的Batch批量处理,
JDBC驱动版本需要5.1.13或以上
测试使用的JDBC驱动版本:mysql-connector-java-5.1.30-bin
测试表结构如下:
CREATE TABLE test (
id int(11) DEFAULT NULL,
name varchar(20) DEFAULT NULL
) ENGINE=InnoDB
首先使用普通的方式插入100万条数据,使用时间81948毫秒
程序如下:
- public class Test {
- public static void main(String[] args) throws ClassNotFoundException,
- SQLException {
- long start = System.currentTimeMillis();
- Class.forName("com.mysql.jdbc.Driver");
- Connection connection = DriverManager
- .getConnection(
- "jdbc:mysql://127.0.0.1:3306/xx",
- "xx", "xx");
- connection.setAutoCommit(false);
- PreparedStatement cmd = connection
- .prepareStatement("insert into test values(?,?)");
-
- for (int i = 0; i < 1000000; i++) {
- cmd.setInt(1, i);
- cmd.setString(2, "test");
- cmd.executeUpdate();
- }
- connection.commit();
-
- cmd.close();
- connection.close();
-
- long end = System.currentTimeMillis();
- System.out.println(end - start);
- }
- }
程序如下:
- public class Test {
- public static void main(String[] args) throws ClassNotFoundException,
- SQLException {
- long start = System.currentTimeMillis();
- Class.forName("com.mysql.jdbc.Driver");
- Connection connection = DriverManager
- .getConnection(
- "jdbc:mysql://127.0.0.1:3306/xx?rewriteBatchedStatements=true",
- "xx", "xx");
- connection.setAutoCommit(false);
- PreparedStatement cmd = connection
- .prepareStatement("insert into test values(?,?)");
-
- for (int i = 0; i < 1000000; i++) {
- cmd.setInt(1, i);
- cmd.setString(2, "test");
- cmd.addBatch();
- if(i%1000==0){
- cmd.executeBatch();
- }
- }
- cmd.executeBatch();
- connection.commit();
-
- cmd.close();
- connection.close();
-
- long end = System.currentTimeMillis();
- System.out.println(end - start);
- }
- }
rewriteBatchedStatements=true
开启MySQL的查询日志general_log,发现如下SQL
INSERT INTO test
VALUES (11, 'test'), (12, 'test'), (13, 'test')......
相对Oracle的批量处理,MySQL需要JDBC参数显式开启,并且对于JDBC驱动的版本也有要求。
0 0
- MySQL使用Batch批量处理
- 批量处理(Batch processing
- ESQL之act=batch批量处理
- jdbc batch批量处理与非批量的比较
- oracle批量(batch)操作的使用
- java mysql 批量处理
- mybatis使用annotation在mysql,oracle上进行批量处理
- MyBatis Batch Update Exception 使用foreach 批量update 出错
- mybatis 使用经验小结,batch批量提交大量数据
- mybatis 使用经验小结,batch批量提交大量数据
- mysql jdbc 批量处理 execteBatch()
- hibernate官方手册学习--第 15 章 批量处理(Batch processing)
- 使用Jdbc进行批量处理
- 使用美图秀秀批量处理照片
- mybatis 使用in批量处理
- mybatis执行批量更新batch update 的方法(oracle,mysql)
- mybatis执行批量更新batch update 的方法(oracle,mysql)
- mybatis执行批量更新batch update 的方法(oracle,mysql)
- 【转载】Sublime Text 2…
- 【原创】mysql 触发器trigger…
- section字符串分割
- 【转载】ubuntu12.04 安装lin…
- 【原创】linux ubuntu12.04&n…
- MySQL使用Batch批量处理
- 国省州市县四级关联表
- 【原创】linux目录突然消失的解决…
- 【原创】Linux ftp lftp
- ubuntu 安装python2.7
- 【原创】 linux Screen 使用
- 解决方案 ERROR: ORA-01033: ORACLE initialization or shutdown in progress
- python 学习系列(3) 读取并显示图片的两种方法
- 【原创】拯救了一次我的破手机