zoukankan      html  css  js  c++  java
  • 关于批量插入数据之我见(100万级别的数据,mysql) (转)

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

    测试数据库为MySQL!!!

    方法一:

    1. public static void insert() {  
    2.         // 开时时间  
    3.         Long begin = new Date().getTime();  
    4.         // sql前缀  
    5.         String prefix = "INSERT INTO tb_big_data (count, create_time, random) VALUES ";  
    6.         try {  
    7.             // 保存sql后缀  
    8.             StringBuffer suffix = new StringBuffer();  
    9.             // 设置事务为非自动提交  
    10.             conn.setAutoCommit(false);  
    11.             // Statement st = conn.createStatement();  
    12.             // 比起st,pst会更好些  
    13.             PreparedStatement pst = conn.prepareStatement("");  
    14.             // 外层循环,总提交事务次数  
    15.             for (int i = 1; i <= 100; i++) {  
    16.                 // 第次提交步长  
    17.                 for (int j = 1; j <= 10000; j++) {  
    18.                     // 构建sql后缀  
    19.                     suffix.append("(" + j * i + ", SYSDATE(), " + i * j  
    20.                             * Math.random() + "),");  
    21.                 }  
    22.                 // 构建完整sql  
    23.                 String sql = prefix + suffix.substring(0, suffix.length() - 1);  
    24.                 // 添加执行sql  
    25.                 pst.addBatch(sql);  
    26.                 // 执行操作  
    27.                 pst.executeBatch();  
    28.                 // 提交事务  
    29.                 conn.commit();  
    30.                 // 清空上一次添加的数据  
    31.                 suffix = new StringBuffer();  
    32.             }  
    33.             // 头等连接  
    34.             pst.close();  
    35.             conn.close();  
    36.         } catch (SQLException e) {  
    37.             e.printStackTrace();  
    38.         }  
    39.         // 结束时间  
    40.         Long end = new Date().getTime();  
    41.         // 耗时  
    42.         System.out.println("cast : " + (end - begin) / 1000 + " ms");  
    43.     }  


    输出时间:cast : 23 ms

    该方法目前测试是效率最高的方法!




    方法二:

    1. public static void insertRelease() {  
    2.         Long begin = new Date().getTime();  
    3.         String sql = "INSERT INTO tb_big_data (count, create_time, random) VALUES (?, SYSDATE(), ?)";  
    4.         try {  
    5.             conn.setAutoCommit(false);  
    6.             PreparedStatement pst = conn.prepareStatement(sql);  
    7.             for (int i = 1; i <= 100; i++) {  
    8.                 for (int k = 1; k <= 10000; k++) {  
    9.                     pst.setLong(1, k * i);  
    10.                     pst.setLong(2, k * i);  
    11.                     pst.addBatch();  
    12.                 }  
    13.                 pst.executeBatch();  
    14.                 conn.commit();  
    15.             }  
    16.             pst.close();  
    17.             conn.close();  
    18.         } catch (SQLException e) {  
    19.             e.printStackTrace();  
    20.         }  
    21.         Long end = new Date().getTime();  
    22.         System.out.println("cast : " + (end - begin) / 1000 + " ms");  
    23.     }  

    注:注释就没有了,和上面类同,下面会有分析!

    控制台输出:cast : 111 ms

    执行时间是上面方法的5倍!



    方法三:

    1. public static void insertBigData(SpringBatchHandler sbh) {  
    2.         Long begin = new Date().getTime();  
    3.         JdbcTemplate jdbcTemplate = sbh.getJdbcTemplate();  
    4.         final int count = 10000;  
    5.         String sql = "INSERT INTO tb_big_data (count, create_time, random) VALUES (?, SYSDATE(), ?)";  
    6.         jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {  
    7.             // 为prepared statement设置参数。这个方法将在整个过程中被调用的次数  
    8.             public void setValues(PreparedStatement pst, int i)  
    9.                     throws SQLException {  
    10.                 pst.setLong(1, i);  
    11.                 pst.setInt(2, i);  
    12.             }  
    13.   
    14.             // 返回更新的结果集条数  
    15.             public int getBatchSize() {  
    16.                 return count;  
    17.             }  
    18.         });  
    19.         Long end = new Date().getTime();  
    20.         System.out.println("cast : " + (end - begin) / 1000 + " ms");  
    21.     }  
  • 相关阅读:
    数据库事务(Transaction)
    取余与取模运算区别
    HashMap与HashTable的区别
    Linux下运行命令出现is not in the sudoers file的问题
    Redis 安装及入门
    Maven本地仓库在C盘下无法自动下载相关依赖的问题
    修改从Maven中心仓库下载到本地的jar包的默认存储位置及远程仓库
    CentOS7使用firewalld管理防火墙与端口
    同时安装Python2,Python3如何解决冲突问题【官方解法】
    数据库查询字段带有特殊字符时报错的问题
  • 原文地址:https://www.cnblogs.com/jpfss/p/7151331.html
Copyright © 2011-2022 走看看