关于批量插入数据之我见(100万级别的数据,mysql) (转)
2023-09-27 14:24:56 时间
因前段时间去面试,问到如何高效向数据库插入10万条记录,之前没处理过类似问题,也没看过相关资料,结果没答上来,今天就查了些资料,总结出三种方法:
方法一:
- public static void insert() {
- // 开时时间
- Long begin = new Date().getTime();
- // sql前缀
- String prefix = "INSERT INTO tb_big_data (count, create_time, random) VALUES ";
- try {
- // 保存sql后缀
- StringBuffer suffix = new StringBuffer();
- // 设置事务为非自动提交
- conn.setAutoCommit(false);
- // Statement st = conn.createStatement();
- // 比起st,pst会更好些
- PreparedStatement pst = conn.prepareStatement("");
- // 外层循环,总提交事务次数
- for (int i = 1; i <= 100; i++) {
- // 第次提交步长
- for (int j = 1; j <= 10000; j++) {
- // 构建sql后缀
- suffix.append("(" + j * i + ", SYSDATE(), " + i * j
- * Math.random() + "),");
- }
- // 构建完整sql
- String sql = prefix + suffix.substring(0, suffix.length() - 1);
- // 添加执行sql
- pst.addBatch(sql);
- // 执行操作
- pst.executeBatch();
- // 提交事务
- conn.commit();
- // 清空上一次添加的数据
- suffix = new StringBuffer();
- }
- // 头等连接
- pst.close();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- // 结束时间
- Long end = new Date().getTime();
- // 耗时
- System.out.println("cast : " + (end - begin) / 1000 + " ms");
- }
输出时间:cast : 23 ms
该方法目前测试是效率最高的方法!
方法二:
- public static void insertRelease() {
- Long begin = new Date().getTime();
- String sql = "INSERT INTO tb_big_data (count, create_time, random) VALUES (?, SYSDATE(), ?)";
- try {
- conn.setAutoCommit(false);
- PreparedStatement pst = conn.prepareStatement(sql);
- for (int i = 1; i <= 100; i++) {
- for (int k = 1; k <= 10000; k++) {
- pst.setLong(1, k * i);
- pst.setLong(2, k * i);
- pst.addBatch();
- }
- pst.executeBatch();
- conn.commit();
- }
- pst.close();
- conn.close();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- Long end = new Date().getTime();
- System.out.println("cast : " + (end - begin) / 1000 + " ms");
- }
注:注释就没有了,和上面类同,下面会有分析!
控制台输出:cast : 111 ms
执行时间是上面方法的5倍!
方法三:
- public static void insertBigData(SpringBatchHandler sbh) {
- Long begin = new Date().getTime();
- JdbcTemplate jdbcTemplate = sbh.getJdbcTemplate();
- final int count = 10000;
- String sql = "INSERT INTO tb_big_data (count, create_time, random) VALUES (?, SYSDATE(), ?)";
- jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
- // 为prepared statement设置参数。这个方法将在整个过程中被调用的次数
- public void setValues(PreparedStatement pst, int i)
- throws SQLException {
- pst.setLong(1, i);
- pst.setInt(2, i);
- }
- // 返回更新的结果集条数
- public int getBatchSize() {
- return count;
- }
- });
- Long end = new Date().getTime();
- System.out.println("cast : " + (end - begin) / 1000 + " ms");
- }
相关文章
- Linux别名设置导致mysql_config_editor配置登录报错
- mysql select/update where id in(上万个元素)的优化方案:
- MySQL事务四个特性 - 事务的四个隔离级别
- Logstash使用jdbc_input同步Mysql数据时遇到的空时间SQLException问题
- MySQL 批量删除表
- MySQL在where后面使用case when
- CentOS7.6 X64搭建mysql服务
- mysql全局唯一ID生成方案(二)
- MySQL数据库的高可用方案总结
- Mysql、Oracle 中的日期格式化比较
- mysql启动參数(/etc/my.cnf)具体解释汇总
- Hive集成Mysql作为元数据时,提示错误:Specified key was too long; max key length is 767 bytes
- 小技巧 之 MySql 一步轻松批量执行 sql 文件
- mysql 获取自增id的值的方法
- mysql中不同事务隔离级别下数据的显示效果--转载
- mysql 批量插入
- [Mysql] CROSS JOIN 交叉连接
- MySQL - 获取当天,昨天,本周,本月,上周,上月的起始时间