mysql 生成测试数据

往mysql数据库中批量数据主要用到两种方法,一种是使用存储过程

create procedure add_data(IN num int)
BEGIN
    DECLARE count INT DEFAULT 0;
    DECLARE id varchar(40);
    WHILE count < num DO
            SET count = count + 1;
            SET id = substring(md5(rand()), 1, 20);
            insert INTO table (id) VALUES (id);
        END WHILE;
END;


SET AUTOCOMMIT=False
call add_data(5000000)
SET AUTOCOMMIT=True

另一种是写java脚本,优点是比较灵活,但速度不够快

@Test
void test() throws SQLException {
    connect = DriverManager.getConnection(host, username, password);
    connect.setAutoCommit(false);

    long start = System.currentTimeMillis();
    insert("risk_info0", 5000000);
    insert("risk_info1", 10000000);
    for (int i = 2; i <= partNum; i++) {
        insert("risk_info" + i, 100000);
    }
    long end = System.currentTimeMillis();
    System.out.println(end - start);

    connect.close();
}

private static void insert(String tableName, int count) throws SQLException {
    String sql = String.format("insert into %s (id) values (?);", tableName);
    PreparedStatement pstmt = connect.prepareStatement(sql);
    try {
        for (int i = 1; i <= count; i++) {
            pstmt.setString(1, RandomStringUtils.randomAlphanumeric(20));
            pstmt.addBatch();
            if (i % 10000 == 0) {
                pstmt.executeBatch();
                connect.commit();
                System.out.println("commit");
            }
        }
        pstmt.executeBatch();
        connect.commit();
    } catch (Exception e) {
        System.out.println("failed! " + e.getMessage());
        return;
    }
    System.out.println("\n<<--表" + tableName + "插入记录" + count + "条-->>");
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值