发表于: 2019-11-24 23:02:55
1 1056
插入数据的java代码
public class InsertTest3 {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Connection conn = null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true&useServerPrepStmts=false&rewriteBatchedStatements=true", "root", "admin");
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 <= 3000; 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 + " s");
}
}
今天完成的事情
准备入学的相关活动(部分)
mysql插入100万和3000万数据,分别在有无索引的情况下
100万无索引:
100万有索引:
3000万无索引:
3000万有索引:
正在跑,太慢了
修改jdbctemplate的代码,为相关方法增加返回值(增删改,返回值:int,boolean),增加log4j的日志配置
明天要做的事情:
继续完成插入1亿数据的插入
完成入学的全部事项
修改好任务一的剩余的相关代码(mybaitsdemo,springmbaits整合),学习使用git和github
评论