当前位置: 首页 > news >正文

网站建设社区交流绵阳专门做网站的公司

网站建设社区交流,绵阳专门做网站的公司,wordpress固定链接设置静态链接,三星网上商城怎么退货SpringBoot中六种批量更新Mysql 方式效率对比 先上结论吧,有空可以自测一下,数据量大时运行一次还时挺耗时的 效率比较 小数据量时6中批量更新效率不太明显,根据项目选择合适的即可,以1万条为准做个效率比较,效率从高到低一次排名如下 replace into和ON DUPLICATE KEY效率最…SpringBoot中六种批量更新Mysql 方式效率对比 先上结论吧,有空可以自测一下,数据量大时运行一次还时挺耗时的 效率比较 小数据量时6中批量更新效率不太明显,根据项目选择合适的即可,以1万条为准做个效率比较,效率从高到低一次排名如下 replace into和ON DUPLICATE KEY效率最高mybatis-plus 有取巧嫌疑,因为是分批批量更新,其他几种都是一次更新for循环凭借sql和JdbcTemplate相近,即使5万条,10万条效率也相近case when 然而有时候我们只能选择case when,因为replace into和ON DUPLICATE KEY公司不一定让用,项目也不一定引入mybatis-plus,数据库url中也不一定有allowMultiQueriestrue参数,算是一个兜底方案吧,不管用那种方式大数据量时都需要考虑分批 测试结构 环境信息:mysql-8.0.35-winx64,本地win 10 依次为测试次数-平均耗时-最小耗时-最大耗时,单位为毫秒 数据量forcase whenreplace intoON DUPLICATE KEYmybatis-plusJdbcTemplate500100-61-41-1202100-66-57-426100-16-10-282100-15-10-293100-73-52-564100-87-59-14491000100-131-94-2018100-241-219-675100-28-18-376100-25-17-331100-117-98-599100-188-136-23975000100-852-735-8297100-11219-10365-13496100-95-83-569100-93-82-552100-618-517-1415100-1161-911-93341000010-3957-2370-1730410-45537-44465-48119100-191-171-762100-188-169-772100-1309-1085-5021100-3671-2563-311125000010-50106-34568-130651卡死不动100-1026-919-1868100-1062-945-1934100-8062-6711-20841100-48744-35482-19101110000010-160170-106223-264434卡死不动10-2551-2292-368810-2503-2173-3579100-17205-14436-2488110-169771-110522-343278 总结 sql语句for循环效率其实相当高的因为它仅仅有一个循环体只不过最后update语句比较多量大了就有可能造成sql阻塞,同时在mysql的url上需要加上allowMultiQueriestrue参数,即 jdbc:mysql://localhost:3306/mysqlTest?characterEncodingutf-8allowMultiQueriestrue(公司项目不一定加,我们也不一定有权限加)。 case when虽然最后只会有一条更新语句但是xml中的循环体有点多每一个case when 都要循环一遍list集合所以大批量拼sql的时候会比较慢所以效率问题严重。使用的时候建议分批插入(我们公司一直用的就是这种,但是必须分批)。 duplicate key update可以看出来是最快的但是公司一般都禁止使用replace into和INSERT INTO … ON DUPLICATE KEY UPDATE这种sql有可能会造成数据丢失和主从上表的自增id值不一致。而且用这个更新时记得一定要加上id而且values括号里面放的是数据库字段不是java对象的属性字段 根据效率安全方面综合考虑选择适合的很重要。 数据库 CREATE TABLE people (id bigint(8) NOT NULL AUTO_INCREMENT,first_name varchar(50) NOT NULL DEFAULT ,last_name varchar(50) NOT NULL DEFAULT ,PRIMARY KEY (id) ) ENGINEInnoDB DEFAULT CHARSETutf8mb4初始化测试数据 //初始化10w数据 Test void init10wData() {for (int i 0; i 100000; i) {People people new People();people.setFirstName(UUID.randomUUID().toString());people.setLastName(UUID.randomUUID().toString());peopleDAO.insert(people);} }批量修改方案 第一种 for !-- 批量更新第一种方法通过接收传进来的参数list进行循环组装sql -- update idupdateBatch parameterTypejava.util.Listforeach collectionlist itemitem indexindex open close separator;update peoplesetif testitem.firstName ! nullfirst_name #{item.firstName,jdbcTypeVARCHAR},/ifif testitem.lastName ! nulllast_name #{item.lastName,jdbcTypeVARCHAR},/if/setwhere id #{item.id,jdbcTypeBIGINT}/foreach /update第二种 case when !-- 批量更新第二种方法通过 case when语句变相的进行批量更新 -- update idupdateBatch2 parameterTypejava.util.Listupdate peoplesetforeach collectionlist itemitemif testitem.firstName ! nullfirst_name case when id #{item.id} then #{item.firstName} else first_name end,/ifif testitem.lastName ! nulllast_name case when id #{item.id} then #{item.lastName} else last_name end,/if/foreach/setwhere id inforeach collectionlist itemitem separator, open( close)#{item.id}/foreach /update第三种 replace into !-- 批量更新第三种方法通过 replace into -- update idupdateBatch3 parameterTypejava.util.Listreplace into people(id,first_name,last_name) valuesforeach collectionlist indexindex itemitem separator,(#{item.id},#{item.firstName},#{item.lastName})/foreach /update第四种 ON DUPLICATE KEY UPDATE !-- 批量更新第四种方法通过 duplicate key update -- update idupdateBatch4 parameterTypejava.util.Listinsert into people(id,first_name,last_name) valuesforeach collectionlist indexindex itemitem separator,(#{item.id},#{item.firstName},#{item.lastName})/foreachON DUPLICATE KEY UPDATEidvalues(id),first_namevalues(first_name),last_namevalues(last_name) /update第五种mybatis-plus提供的的批量更新 default boolean updateBatchById(CollectionT entityList) {return this.updateBatchById(entityList, 1000); } boolean updateBatchById(CollectionT entityList, int batchSize);mybatis-plus提供的批量更新是分批批量更新,默认每批1000条,可以指定分批的条数,每批执行完成后提交一下事务,不加Transactional可能会出现第一批更新成功了,第二批更新失败了的情况. 第六种JdbcTemplate提供的批量更新 测试代码 /*** PeopleDAO继承基类*/ Mapper Repository public interface PeopleDAO extends MyBatisBaseDaoPeople, Long {void updateBatch(Param(list) ListPeople list);void updateBatch2(ListPeople list);void updateBatch3(ListPeople list);void updateBatch4(ListPeople list); } SpringBootTest class PeopleMapperTest {ResourcePeopleMapper peopleMapper;ResourcePeopleService peopleService;ResourceJdbcTemplate jdbcTemplate;Testvoid init10wData() {for (int i 0; i 100000; i) {People people new People();people.setFirstName(UUID.randomUUID().toString());people.setLastName(UUID.randomUUID().toString());peopleMapper.insert(people);}}Testvoid updateBatch() {ListPeople list new ArrayList();int loop 100;int count 5000;Long maxCost 0L;//最长耗时Long minCost Long.valueOf(Integer.MAX_VALUE);//最短耗时for (int j 0; j count; j) {People people new People();people.setId(ThreadLocalRandom.current().nextInt(0, 100000));people.setFirstName(UUID.randomUUID().toString());people.setLastName(UUID.randomUUID().toString());list.add(people);}Long startTime System.currentTimeMillis();for (int i 0; i loop; i) {Long curStartTime System.currentTimeMillis();// peopleMapper.updateBatch4(list);// peopleService.updateBatchById(list);jdbcTemplateBatchUpdate(list);Long curCostTime System.currentTimeMillis() - curStartTime;if (maxCost curCostTime) {maxCost curCostTime;}if (minCost curCostTime) {minCost curCostTime;}}System.out.println(loop - (System.currentTimeMillis() - startTime) / loop - minCost - maxCost );}private void jdbcTemplateBatchUpdate (ListPeople list){String sql update people set first_name?,last_name? where id ?;ListObject[] params list.stream().map(item - new Object[]{item.getFirstName(), item.getLastName(), item.getId()}).collect(Collectors.toList());jdbcTemplate.batchUpdate(sql,params);} }参考文章 mybatis批量更新数据三种方法效率对比 https://blog.csdn.net/q957967519/article/details/88669552 MySql中4种批量更新的方法 https://blog.csdn.net/weixin_42290280/article/details/89384741 Mysql 批量修改四种方式效率对比一https://blog.csdn.net/zk673820543/article/details/106579809/
http://www.dnsts.com.cn/news/252556.html

相关文章:

  • 西安外贸网站开发百度搜索引擎怎么做
  • 做化学合成的网站有哪些亚马逊在哪个网站做推广
  • 上海网站建设哪家企业沈阳网站建设服务器
  • 网站维护教程佛山网站优化公司排名
  • 电子商务网站建设成都受欢迎的常州做网站
  • 什么秀网站做效果图wordpress恢复设置
  • 建设微信营销网站制作上海微信小程序网站建设
  • 精品成品网站1688凡客诚品购物
  • 个人网站介绍模板下载百度资源搜索平台官网
  • 网站建设公司倒闭网站登不了企业建设网银
  • 做短视频网站收益常州市住房和城乡建设局网站
  • 炫酷的网站设计软件发布流程
  • 浙江省城乡建设网站王也配音演员
  • 漳州网站建设哪家最正规西安网站建设 中讯创赢
  • 交互网站开发培训公众号模板编辑器
  • 万网网站建设步骤专门做餐饮ppt的网站
  • 营销网站的类型网站开发包括网站设计
  • 管理部门网站建设说明北郊网站建设公司
  • 建设静态网站工具在线室内设计工具
  • 门户网站建设请示抖音seo公司
  • 电子商务网站调研报告开源中国
  • 管理咨询网站建设dedecms做的系统网站主页是哪一个文件
  • 网络科技网站建设阳江网站建设公司
  • 池州网站公司金华英文网站建设
  • 上海医疗 网站制作建设网站都需要哪些内容
  • 义乌外贸公司网站百度竞价推广效果好吗
  • 网站开发多长时间阜宁城乡建设局网站
  • 上海市网站开发公司排名大连seo网站
  • 禅城做网站网站备案撤销
  • 网站区域名是什么单位做网站需要准备什么