运行环境:
使用数据库sqlserver,JDK版本1.8,运行在SpringBoot对比环境下的三种可用方法:
反复执行单条插入语句
xml拼接sql
批处理执行
先说结论:少量插入请反复插入单个数据,方便。请使用批处理。
(可以考虑以20个左右的需求插入数据为界,在我的测试和数据库环境下耗时100毫秒,方便性最重要)。任何时候都不需要xml拼接sql的方式。
拼接SQL的xmlnewId()是sqlserver生成UUID函数与本文内容无关
<insertid="insertByBatch"parameterType="java.util.List"> INSERTINTOtb_itemVALUES <foreachcollection="list"item="item"index="index"separator=","> (newId(),#{item.uniqueCode},#{item.projectId},#{item.name},#{item.type},#{item.packageUnique}, #{item.isPackage},#{item.factoryId},#{item.projectName},#{item.spec},#{item.length},#{item.weight}, #{item.material},#{item.setupPosition},#{item.areaPosition},#{item.bottomHeight},#{item.topHeight}, #{item.serialNumber},#{item.createTime}</foreach> </insert>
Mapper是 mybatis插件tk.Mapper 接口与本文内容关系不大
publicinterfaceItemMapperextendsMapper<Item>{ intinsertByBatch(List<Item>itemList); }
Service类
@Service publicclassItemService{ @Autowired privateItemMapperitemMapper; @Autowired privateSqlSessionFactorysqlSessionFactory; //批处理 @Transactional publicvoidadd(List<Item>itemList){ SqlSessionsession=sqlSessionFactory.openSession(ExecutorType.BATCH,false); ItemMappermapper=session.getMapper(ItemMapper.class); for(inti=0;i<itemList.size();i ){ mapper.insertSelective(itemList.get(i)); if(i00==999){//每1000条提交一次防止内存溢出 session.commit(); session.clearCache(); } } session.commit(); session.clearCache(); } //拼接sql @Transactional publicvoidadd1(List<Item>itemList){ itemList.insertByBatch(itemMapper::insertSelective); } //循环插入 @Transactional publicvoidadd2(List<Item>itemList){ itemList.forEach(itemMapper::insertSelective); } }
测试类
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment=SpringBootTest.WebEnvironment.RANDOM_PORT,classes=ApplicationBoot.class) publicclassItemServiceTest{ @Autowired ItemServiceitemService; privateList<Item>itemList=newArrayList<>(); //生成测试List @Before publicvoidcreateList(){ Stringjson="{\n" "\"areaPosition\":\"TEST\",\n" &bsp; " \"bottomHeight\": 5,\n" +
" \"factoryId\": \"0\",\n" +
" \"length\": 233.233,\n" +
" \"material\": \"Q345B\",\n" +
" \"name\": \"TEST\",\n" +
" \"package\": false,\n" +
" \"packageUnique\": \"45f8a0ba0bf048839df85f32ebe5bb81\",\n" +
" \"projectId\": \"094b5eb5e0384bb1aaa822880a428b6d\",\n" +
" \"projectName\": \"项目_TEST1\",\n" +
" \"serialNumber\": \"1/2\",\n" +
" \"setupPosition\": \"1B柱\",\n" +
" \"spec\": \"200X200X200\",\n" +
" \"topHeight\": 10,\n" +
" \"type\": \"Steel\",\n" +
" \"uniqueCode\": \"12344312\",\n" +
" \"weight\": 100\n" +
" }";
Item test1 = JSON.parseObject(json,Item.class);
test1.setCreateTime(new Date());
for (int i = 0; i < 1000; i++) {//测试会修改此数量
itemList.add(test1);
}
}
//批处理
@Test
@Transactional
public void tesInsert() {
itemService.add(itemList);
}
//拼接字符串
@Test
@Transactional
public void testInsert1(){
itemService.add1(itemList);
}
//循环插入
@Test
@Transactional
public void testInsert2(){
itemService.add2(itemList);
}
}
测试结果:
10条 25条数据插入经多次测试,波动性较大,但基本都在百毫秒级别
其中 拼接sql方式在插入500条和1000条时报错(似乎是因为sql语句过长,此条跟数据库类型有关,未做其他数据库的测试):
com.microsoft.sqlserver.jdbc.SQLServerException: 传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确。此 RPC 请求中提供了过多的参数。最多应为 2100可以发现
循环插入的时间复杂度是 O(n),并且常数C很大
拼接SQL插入的时间复杂度(应该)是 O(logn),但是成功完成次数不多,不确定
批处理的效率的时间复杂度是 O(logn),并且常数C也比较小
循环插入单条数据虽然效率极低,但是代码量极少,在使用tk.Mapper的插件情况下,仅需代码,:
@Transactional
public void add1(List<Item> itemList) {
itemList.forEach(itemMapper::insertSelective);
}
因此,在需求插入数据数量不多的情况下肯定用它了。
xml拼接sql是最不推荐的方式,使用时有大段的xml和sql语句要写,很容易出错,工作效率很低。
更关键点是,虽然效率尚可,但是真正需要效率的时候你挂了,要你何用?批处理执行是有大数据量插入时推荐的做法,使用起来也比较方便。
IT技术分享社区
个人博客网站:https://programmerblog.xyz
文章推荐程序员效率:画流程图常用的工具程序员效率:整理常用的在线笔记软件远程办公:常用的远程协助软件,你都知道吗?51单片机程序下载、ISP及串口基础知识硬件:断路器、接触器、继电器基础知识