快速向数据库中插入千万级伪数据

一、MYSQL版(约半小时插入2k500w条记录,本地内存8G)

  MYSQL 批量插入SQL格式

INSERT INTO TABLE_NAME (...) VALUES (...),(...),(...)

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

public class BatchInsert {
    static Connection conn = null;
    public static void initConn() {
        String url = "jdbc:mysql://localhost:3306/test?"
                + "user=root&password=5555&useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=UTC";
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void insert() {
        // 开时时间
        Long begin = new Date().getTime();
        // sql前缀
        String prefix = "INSERT INTO stu_info (...) 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 <= 2500; i++) {
                // 一次批量插入数据数,这里设置成10000正常运行,10w则报错,应该可以通过配置MYSQL参数增加批量插入的数据量
                for (int j = 1; j <= 10000; j++) {
                    // 构建sql后缀
                    suffix.append("(" ... "),"); //   ...对应全部字段值
                }
                // 构建完整sql
                String sql = prefix + suffix.substring(0, suffix.length() - 1);
                // 添加执行sql
                System.out.println(new Date().toString()+",已经插入记录条数:"+(i*10000));
                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");
    }
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        initConn();
        insert();
    }
}

2、ORACLE版(本地测试 ORACLE插入千条记录效率不及MYSQL插入万条记录)

  MYSQL 批量插入SQL格式

INSERT ALL INTO TABLE_NAME (...) VALUES (...)

INTO TABLE_NAME(...) VALUES(...)

INTO TABLE_NAME(...) VALUES(...)

...

...

...

import java.sql.*;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
/**
 * @program: pthink-generator->TestNGInsert
 * @description:
 * @author: WuDG
 * @create: 2019-08-14 00:51
 **/
@SuppressWarnings("all")
public class TestNGInsert {
    static Connection conn = null;
    public static void initConn() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "";
            String username = "";
            String password = "";
            conn = DriverManager.getConnection(url, username, password);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static void insert() {
        // 开时时间
        Long begin = new Date().getTime();
        // sql前缀
        String prefix = "INSERT ALL ";
        try {
            // 保存sql后缀
            StringBuffer suffix = new StringBuffer();
            // 设置事务为非自动提交
            conn.setAutoCommit(false);
            Statement st = conn.createStatement();// 外层循环,总提交事务次数
            for (int i = 1; i <= 25000; i++) {
                // 一次批量插入数据数,这里设置成1000正常运行,1w则报错,应该可以通过配置MYSQL参数增加批量插入的数据量
                for (int j = 1; j <= 1000; j++) {
                    // 构建sql后缀
                    suffix.append("INTO table_name(...) VALUES (" ...") ");
                }
                // 构建完整sql
                String sql = prefix + suffix + " select * from dual";// 添加执行sql
                st.addBatch(sql);
                // 执行操作
                st.executeBatch();
                // 提交事务
                conn.commit();
                System.out.println(new Date().toString() + ",已经插入记录条数:" + (i * 1000));
                // 清空上一次添加的数据
                suffix = new StringBuffer();
            }
            st.close();
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        // 结束时间
        Long end = new Date().getTime();
        // 耗时
        System.out.println("COST : " + (end - begin) / 1000 + " s");
    }
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
        initConn();
        insert();
    }
}

 

转载于:https://www.cnblogs.com/RoronoaZoro/p/11351778.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值