JEECGBOOT-如何提高導入 批量插入效率幾點建議
@author ksf
幾種模式下的請求響應:
插入一萬條數據,耗時情況ms:
49,271 > 3,824 > 2,477 >818
默認情況下:
/**
* ----- testMybatisInsert100000Save method test ------start:1593313133697
* ----- testMybatisInsert100000Save method test ------end: 1593313182968
* 49,271
*/
@Test
public void testMybatisInsert100000Save() {
List<JeecgDemo> jeecgDemoList = initDemos();
System.out.println(("----- testMybatisInsert100000Save method test ------start:" + System.currentTimeMillis()));
jeecgDemoList.forEach(jeecgDemo -> {
jeecgDemoMapper.insert(jeecgDemo);
});
System.out.println(("----- testMybatisInsert100000Save method test ------end: " + System.currentTimeMillis()));
}
批量保存的情況
----- testMybatisInsert100000BatchSave method test ------start:1593312989804
----- testMybatisInsert100000BatchSave method test ------end: 1593312992281
2,477
@Test
public void testMybatisInsert100000BatchSave() {
List<JeecgDemo> jeecgDemoList = initDemos();
System.out.println(("----- testMybatisInsert100000BatchSave method test ------start:" + System.currentTimeMillis()));
jeecgDemoMapper.insertBatch(jeecgDemoList);
System.out.println(("----- testMybatisInsert100000BatchSave method test ------end: " + System.currentTimeMillis()));
}
public Integer insertBatch(List<JeecgDemo> list);
<insert id="insertBatch">
INSERT INTO `demo`( `id`, `name`,
`key_word`,
`punch_time`,
`salary_money`,
`bonus_money`,
`sex`, `age`, `birthday`,
`email`, `content`)
VALUES
<foreach collection ="list" item="demo" separator =",">
( #{demo.id}, #{demo.name}, #{demo.keyWord},
#{demo.punchTime}, #{demo.salaryMoney}, #{demo.bonusMoney},
#{demo.sex}, #{demo.age}, #{demo.birthday},
#{demo.email}, #{demo.content} )
</foreach >
</insert>
**Mybatis 自帶批量保存**
/**
* ----- testMybatisInsert100000SqlSessionBatchSave method test ------start:1593313533345
* ----- testMybatisInsert100000SqlSessionBatchSave method test ------end: 1593313537169
* 3,824
*/
@Test
public void testMybatisInsert100000SqlSessionBatchSave() {
List<JeecgDemo> jeecgDemoList = initDemos();
System.out.println(("----- testMybatisInsert100000SqlSessionBatchSave method test ------start:" + System.currentTimeMillis()));
SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH.BATCH, false);
JeecgDemoMapper jeecgDemoMapper = sqlSession.getMapper(JeecgDemoMapper.class);
jeecgDemoList.forEach(jeecgDemo -> {
jeecgDemoMapper.insert(jeecgDemo);
});
sqlSession.commit();
System.out.println(("----- testMybatisInsert100000SqlSessionBatchSave method test ------end: " + System.currentTimeMillis()));
}
----- testJdbcInsert100000BatchSave method test ------start:1593315311322
2020-06-28 11:35:11.329 [main] INFO com.alibaba.druid.pool.DruidDataSource:1003 - {dataSource-2} inited
----- testJdbcInsert100000BatchSave method test ------end: 1593315312140
818
***需要數據庫支持批量操作: &allowMultiQueries=true&rewriteBatchedStatements=true***
url: jdbc:mysql://127.0.0.1:3306/jeecg-boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowMultiQueries=true&rewriteBatchedStatements=true
@Test
public void testJdbcInsert100000BatchSave() {
List<Object[]> jeecgDemoList = initJDBCDemos();
DruidDataSource dataSource = DynamicDBUtil.getDbSourceByDbKey("master");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
System.out.println(("----- testJdbcInsert100000BatchSave method test ------start:" + System.currentTimeMillis()));
String sql ="INSERT INTO `demo`( `id`, `name`,\n" +
"\t\t`key_word`,\n" +
"\t\t`punch_time`,\n" +
"\t\t `salary_money`,\n" +
"\t\t `bonus_money`,\n" +
"\t\t `sex`, `age`, `birthday`,\n" +
"\t\t `email`, `content`)\n" +
"\t\tVALUES (?,?,?,?,?,?,?,?,?,?,?)";
jdbcTemplate.batchUpdate(sql,jeecgDemoList);
System.out.println(("----- testJdbcInsert100000BatchSave method test ------end: " + System.currentTimeMillis()));
}
初始化數據
public List<JeecgDemo> initDemos(){
List<JeecgDemo> demos = new ArrayList<>();
for (int i = 0; i < 1000000; i++) {
JeecgDemo demo = new JeecgDemo();
demo.setSysOrgCode(i+"");
demo.setName(i+"name");
demo.setKeyWord(i+"keyWord");
demo.setPunchTime(new Date());
demo.setSalaryMoney(BigDecimal.ONE);
demo.setBonusMoney(1d);
demo.setSex("1");
demo.setAge(10);
demo.setBirthday(new Date());
demo.setEmail("[email protected]");
demo.setContent("[email protected]");
demos.add(demo);
}
return demos;
}
public List<Object[]> initJDBCDemos(){
List<Object[]> demos = new ArrayList<>();
for (int i = 0; i < 10000; i++) {
Object[] demo = new Object[11];
demo[0] =i+""+new Date();
demo[1] =i+"name";
demo[2] =i+"keyWord";
demo[3] =new Date();
demo[4] =BigDecimal.ONE;
demo[5] =1d;
demo[6] ="1";
demo[7] =10;
demo[8] =new Date();
demo[9] ="[email protected]";
demo[10] ="[email protected]";
demos.add(demo);
}
return demos;
}