给你2万条数据,怎么快速导入到MySQL?写得太好了
一、前言
前两天做了一个导入的功能,导入开始的时候非常慢,导入2w条数据要1分多钟,后来一点一点的优化,从直接把list怼进Mysql中,到分配把list导入Mysql中,到多线程把list导入Mysql中。
时间是一点一点的变少了。非常的爽,最后变成了10s以内。
下面就展示一下过程。 二、直接把list怼进Mysql
使用mybatis的批量导入操作: @Transactional(rollbackFor = Exception.class) public int addFreshStudentsNew2(List list, String schoolNo) { if (list == null || list.isEmpty()) { return 0; } List studentEntityList = new LinkedList<>(); List enrollStudentEntityList = new LinkedList<>(); List allusersEntityList = new LinkedList<>(); for (FreshStudentAndStudentModel freshStudentAndStudentModel : list) { EnrollStudentEntity enrollStudentEntity = new EnrollStudentEntity(); StudentEntity studentEntity = new StudentEntity(); BeanUtils.copyProperties(freshStudentAndStudentModel, studentEntity); BeanUtils.copyProperties(freshStudentAndStudentModel, enrollStudentEntity); String operator = TenancyContext.UserID.get(); String studentId = BaseUuidUtils.base58Uuid(); enrollStudentEntity.setId(BaseUuidUtils.base58Uuid()); enrollStudentEntity.setStudentId(studentId); enrollStudentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard()); enrollStudentEntity.setOperator(operator); studentEntity.setId(studentId); studentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard()); studentEntity.setOperator(operator); studentEntityList.add(studentEntity); enrollStudentEntityList.add(enrollStudentEntity); AllusersEntity allusersEntity = new AllusersEntity(); allusersEntity.setId(enrollStudentEntity.getId()); allusersEntity.setUserCode(enrollStudentEntity.getNemtCode()); allusersEntity.setUserName(enrollStudentEntity.getName()); allusersEntity.setSchoolNo(schoolNo); allusersEntity.setTelNum(enrollStudentEntity.getTelNum()); allusersEntity.setPassword(enrollStudentEntity.getNemtCode()); //密码设置为考生号 allusersEntityList.add(allusersEntity); } enResult = enrollStudentDao.insertAll(enrollStudentEntityList); stuResult = studentDao.insertAll(studentEntityList); allResult = allusersFacade.insertUserList(allusersEntityList); if (enResult > 0 && stuResult > 0 && allResult) { return 10; } return -10; }
Mapper.xml insert into tb_enroll_student id, remark, nEMT_aspiration, nEMT_code, nEMT_score, student_id, identity_card_id, level, major, name, nation, secondary_college, operator, sex, is_delete, account_address, native_place, original_place, used_name, pictrue, join_party_date, political_status, tel_num, is_registry, graduate_school, create_time, update_time values ( #{item.id,jdbcType=VARCHAR}, #{item.remark,jdbcType=VARCHAR}, #{item.nemtAspiration,jdbcType=VARCHAR}, #{item.nemtCode,jdbcType=VARCHAR}, #{item.nemtScore,jdbcType=VARCHAR}, #{item.studentId,jdbcType=VARCHAR}, #{item.identityCardId,jdbcType=VARCHAR}, #{item.level,jdbcType=VARCHAR}, #{item.major,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR}, #{item.nation,jdbcType=VARCHAR}, #{item.secondaryCollege,jdbcType=VARCHAR}, #{item.operator,jdbcType=VARCHAR}, #{item.sex,jdbcType=VARCHAR}, 0, #{item.accountAddress,jdbcType=VARCHAR}, #{item.nativePlace,jdbcType=VARCHAR}, #{item.originalPlace,jdbcType=VARCHAR}, #{item.usedName,jdbcType=VARCHAR}, #{item.pictrue,jdbcType=VARCHAR}, #{item.joinPartyDate,jdbcType=VARCHAR}, #{item.politicalStatus,jdbcType=VARCHAR}, #{item.telNum,jdbcType=VARCHAR}, #{item.isRegistry,jdbcType=TINYINT}, #{item.graduateSchool,jdbcType=VARCHAR}, now(), now() )
代码说明:
底层的mapper是通过逆向工程来生成的,批量插入如下,是拼接成类似: insert into tb_enroll_student()values (),()…….();
这样的缺点是,数据库一般有一个默认的设置,就是每次sql操作的数据不能超过4M。这样插入,数据多的时候,数据库会报错 Packet for query is too large (6071393 > 4194304). You can change this value on the server by setting the max_allowed_packet" variable., 虽然我们可以通过
类似 修改 my.ini 加上 max_allowed_packet =67108864 ,67108864=64M ,默认大小4194304 也就是4M
修改完成之后要重启mysql服务,如果通过命令行修改就不用重启mysql服务。
完成本次操作,但是我们不能保证项目单次最大的大小是多少,这样是有弊端的。所以可以考虑进行分组导入。 三、分组把list导入Mysql中
同样适用mybatis批量插入,区别是对每次的导入进行分组计算,然后分多次进行导入: @Transactional(rollbackFor = Exception.class) public int addFreshStudentsNew2(List list, String schoolNo) { if (list == null || list.isEmpty()) { return 0; } List studentEntityList = new LinkedList<>(); List enrollStudentEntityList = new LinkedList<>(); List allusersEntityList = new LinkedList<>(); for (FreshStudentAndStudentModel freshStudentAndStudentModel : list) { EnrollStudentEntity enrollStudentEntity = new EnrollStudentEntity(); StudentEntity studentEntity = new StudentEntity(); BeanUtils.copyProperties(freshStudentAndStudentModel, studentEntity); BeanUtils.copyProperties(freshStudentAndStudentModel, enrollStudentEntity); String operator = TenancyContext.UserID.get(); String studentId = BaseUuidUtils.base58Uuid(); enrollStudentEntity.setId(BaseUuidUtils.base58Uuid()); enrollStudentEntity.setStudentId(studentId); enrollStudentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard()); enrollStudentEntity.setOperator(operator); studentEntity.setId(studentId); studentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard()); studentEntity.setOperator(operator); studentEntityList.add(studentEntity); enrollStudentEntityList.add(enrollStudentEntity); AllusersEntity allusersEntity = new AllusersEntity(); allusersEntity.setId(enrollStudentEntity.getId()); allusersEntity.setUserCode(enrollStudentEntity.getNemtCode()); allusersEntity.setUserName(enrollStudentEntity.getName()); allusersEntity.setSchoolNo(schoolNo); allusersEntity.setTelNum(enrollStudentEntity.getTelNum()); allusersEntity.setPassword(enrollStudentEntity.getNemtCode()); //密码设置为考生号 allusersEntityList.add(allusersEntity); } int c = 100; int b = enrollStudentEntityList.size() / c; int d = enrollStudentEntityList.size() % c; int enResult = 0; int stuResult = 0; boolean allResult = false; for (int e = c; e <= c * b; e = e + c) { enResult = enrollStudentDao.insertAll(enrollStudentEntityList.subList(e - c, e)); stuResult = studentDao.insertAll(studentEntityList.subList(e - c, e)); allResult = allusersFacade.insertUserList(allusersEntityList.subList(e - c, e)); } if (d != 0) { enResult = enrollStudentDao.insertAll(enrollStudentEntityList.subList(c * b, enrollStudentEntityList.size())); stuResult = studentDao.insertAll(studentEntityList.subList(c * b, studentEntityList.size())); allResult = allusersFacade.insertUserList(allusersEntityList.subList(c * b, allusersEntityList.size())); } if (enResult > 0 && stuResult > 0 && allResult) { return 10; } return -10; }
代码说明:
这样操作,可以避免上面的错误,但是分多次插入,无形中就增加了操作实践,很容易超时。所以这种方法还是不值得提倡的。
再次改进,使用多线程分批导入。 四、多线程分批导入Mysql
依然使用mybatis的批量导入,不同的是,根据线程数目进行分组,然后再建立多线程池,进行导入。 @Transactional(rollbackFor = Exception.class) public int addFreshStudentsNew(List list, String schoolNo) { if (list == null || list.isEmpty()) { return 0; } List studentEntityList = new LinkedList<>(); List enrollStudentEntityList = new LinkedList<>(); List allusersEntityList = new LinkedList<>(); list.forEach(freshStudentAndStudentModel -> { EnrollStudentEntity enrollStudentEntity = new EnrollStudentEntity(); StudentEntity studentEntity = new StudentEntity(); BeanUtils.copyProperties(freshStudentAndStudentModel, studentEntity); BeanUtils.copyProperties(freshStudentAndStudentModel, enrollStudentEntity); String operator = TenancyContext.UserID.get(); String studentId = BaseUuidUtils.base58Uuid(); enrollStudentEntity.setId(BaseUuidUtils.base58Uuid()); enrollStudentEntity.setStudentId(studentId); enrollStudentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard()); enrollStudentEntity.setOperator(operator); studentEntity.setId(studentId); studentEntity.setIdentityCardId(freshStudentAndStudentModel.getIdCard()); studentEntity.setOperator(operator); studentEntityList.add(studentEntity); enrollStudentEntityList.add(enrollStudentEntity); AllusersEntity allusersEntity = new AllusersEntity(); allusersEntity.setId(enrollStudentEntity.getId()); allusersEntity.setUserCode(enrollStudentEntity.getNemtCode()); allusersEntity.setUserName(enrollStudentEntity.getName()); allusersEntity.setSchoolNo(schoolNo); allusersEntity.setTelNum(enrollStudentEntity.getTelNum()); allusersEntity.setPassword(enrollStudentEntity.getNemtCode()); //密码设置为考生号 allusersEntityList.add(allusersEntity); }); int nThreads = 50; int size = enrollStudentEntityList.size(); ExecutorService executorService = Executors.newFixedThreadPool(nThreads); List> futures = new ArrayList>(nThreads); for (int i = 0; i < nThreads; i++) { final List EnrollStudentEntityImputList = enrollStudentEntityList.subList(size / nThreads * i, size / nThreads * (i + 1)); final List studentEntityImportList = studentEntityList.subList(size / nThreads * i, size / nThreads * (i + 1)); final List allusersEntityImportList = allusersEntityList.subList(size / nThreads * i, size / nThreads * (i + 1)); Callable task1 = () -> { studentSave.saveStudent(EnrollStudentEntityImputList,studentEntityImportList,allusersEntityImportList); return 1; }; futures.add(executorService.submit(task1)); } executorService.shutdown(); if (!futures.isEmpty() && futures != null) { return 10; } return -10; }
代码说明:
上面是通过应用ExecutorService 建立了固定的线程数,然后根据线程数目进行分组,批量依次导入。一方面可以缓解数据库的压力,另一个面线程数目多了,一定程度会提高程序运行的时间。
缺点就是要看服务器的配置,如果配置好的话就可以开多点线程,配置差的话就开小点。
来源:blog.csdn.net/kisscatforever/article/details/79817039
大变局下,如何安放好我们的财富大家好,我是老李。如果让我为2020年选择一个关键字,大变局可能是最合适的。2020年,全球犹如被风暴席卷过后一般残破不堪,海外新冠肺炎疫情尚未得到有效控制中美贸易摩擦继续恶化,经
财富自由,这个时代的骗局大家好,我是老李。这几年,财富自由的鸡汤故事愈发层出不穷我朋友85年的,30岁之前就靠做生意实现了财务自由。我90后,普通家庭自己创业开服装厂,买了四套房,算财务自由了吧。我爸爸没
央行发出最后通牒!房贷利率到底要不要转LPR?大家好,我是老李。相信最近有房贷的朋友基本都收到了一条短信来自于房贷银行的最后通牒。以建行为例,从8月25日起对房贷批量转换,从老合同转为LPR定价。不愿意转换的,可以在9月1日至
如今的你还想移民吗?大家好,我是老李。老李我最近认识一个新朋友Alex,是一个比较年轻的男生,不过从业经历还算丰富,最在财富管理公司工作了几年,2年前转行去了国内排名前三的移民公司做起了移民顾问,做的
丈母娘招女婿要求年化收益10,婚姻财富两手都要抓两手都要硬大家好,我是老李。最近,在上海著名的相亲角,有一位上海阿姨招女婿的条件火了先说清楚,这可不是段子,而是长期驻扎相亲角的某上海阿姨的真实诉求。那么我们今天就来就这一要求来好好探讨一番
买保险真的那么重要吗?大家好,我是老李。今天又聊回我的主业保险了,从业这几年来,其实老李我一直在思考保险到底是什么?保险的本质是什么?众所周知,保险是一种转移风险损失的金融工具,其本身并不具备抵御风险的
在巨大利益面前,不要试图考验亲情大家好,我是老李。生老病死这样子的一个轮回,是我们每个人都不可避免的。现在很多人都能开诚布公地谈论生死,过去,中国人都说养儿防老,但如今的社会形式却让许多人不禁感叹养老防儿!今天我
我们到底想把钱变成什么?大家好,我是老李。不知道各位朋友有没有想过这么一个问题,我们赚到的钱,也就是我们手里的钱,在最终都需要一个去处。可以说这些最终的去处就是我们的终极资产,那么我们到底有想好要把钱变成
小米有品电动小玩具,貌比潘安,4模式12功能,老罗直呼内行男人似乎天生就有喜爱科技与多用途工具的特性,除了关注手机AI智能机器人等各种科技产品,平时还喜欢收集一些玩具,在生活中这些工具也是能帮上不小的忙,没事还能拿出来把玩一下,男人的快乐
小米有品小钢炮,转速220rpm,5个鸡蛋轻,续航达一年说起男人对工具的执念,无异于女人对爱马仕香奈儿LV等包包以及雅诗兰黛兰蔻迪奥等化妆品的渴望。不管你爱不爱干活,压箱里的工具还是得备上一套。在众多的工具中,螺丝刀应该算得上使用频率比
AMD全面翻身,依靠Ryzen超车Intelr,成为世界第一如今半导体的时代变了,英特尔凭借多年的技术优势持续占据垄断优势,可大家在其一家独大的环境里也憋得太久了,都想冒头透透气。如今台积电和三星都准备在今年风险量产3nm了,先前卡10nm