java sql 测试批量插入效率
四种模式下的批量插入测试响应:
插入一万条数据,耗时情况ms:
[{ "taskName": "循环插入", "timeMillis": 20771, "timeSeconds": 20.771 }, { "taskName": "批量保存", "timeMillis": 1903, "timeSeconds": 1.903 }, { "taskName": "Mybatis自带批量保存", "timeMillis": 12737, "timeSeconds": 12.737 }, { "taskName": "spring jdbcTemplate", "timeMillis": 11582, "timeSeconds": 11.582 }]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | /** * 默认情况,循环插入 * 14.312 */ public void testMybatisInsertSave(Integer num) { List<BatchInsertDemo> batchInsertDemoList = initDemos(num); batchInsertDemoList.forEach(batchInsertDemo -> { batchInsertDemoMapper.insert(batchInsertDemo); }); } /** * 批量保存的情况 * 1.177 */ public void testMybatisInsertBatchSave(Integer num) { List<BatchInsertDemo> batchInsertDemoList = initDemos(num); batchInsertDemoMapper.insertBatch(batchInsertDemoList); } /** * Mybatis 自带批量保存 * 8.087 */ public void testMybatisInsertSqlSessionBatchSave(Integer num) { List<BatchInsertDemo> batchInsertDemoList = initDemos(num); SqlSession sqlSession = sqlSessionTemplate.getSqlSessionFactory().openSession(ExecutorType.BATCH, false ); BatchInsertDemoMapper batchInsertDemoMapper = sqlSession.getMapper(BatchInsertDemoMapper. class ); batchInsertDemoList.forEach(batchInsertDemo -> { batchInsertDemoMapper.insert(batchInsertDemo); }); sqlSession.commit(); } /** * spring jdbcTemplate * 7.132 */ public void testJdbcInsertBatchSave(Integer num) { List<Object[]> demoList = initJDBCDemos(num); 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, demoList); } |
https://gitee.com/caoyeoo0/xc-springboot/blob/mybatis/批量插入/src/main/java/com/xc/xcspringboot/service/impl/BatchInsertDemoServiceImpl.java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律
2021-12-07 es 布尔查询
2021-12-07 js 打印 去除页眉页脚
2017-12-07 bootstrap 栅栏系统
2016-12-07 input placeholder属性 样式修改(颜色,大小,位置)
2016-12-07 div+css:div中图片垂直居中
2016-12-07 div+css:两个div并排等高 (table-cell)