纠纷奇闻社交美文家庭
投稿投诉
家庭城市
爱好生活
创业男女
能力餐饮
美文职业
心理周易
母婴奇趣
两性技能
社交传统
新闻范文
工作个人
思考社会
作文职场
家居中考
兴趣安全
解密魅力
奇闻笑话
写作笔记
阅读企业
饮食时事
纠纷案例
初中历史
说说童话
乐趣治疗

给你2万条数据,怎么快速导入到MySQL?写得太好了

1月17日 星宿房投稿
  一、前言
  前两天做了一个导入的功能,导入开始的时候非常慢,导入2w条数据要1分多钟,后来一点一点的优化,从直接把list怼进Mysql中,到分配把list导入Mysql中,到多线程把list导入Mysql中。
  时间是一点一点的变少了。非常的爽,最后变成了10s以内。
  下面就展示一下过程。二、直接把list怼进Mysql
  使用mybatis的批量导入操作:Transactional(rollbackForException。class)publicintaddFreshStudentsNew2(ListFreshStudentAndStudentModellist,StringschoolNo){if(listnulllist。isEmpty()){return0;}ListStudentEntitystudentEntityListnewLinkedList();ListEnrollStudentEntityenrollStudentEntityListnewLinkedList();ListallusersEntityListnewLinkedList();for(FreshStudentAndStudentModelfreshStudentAndStudentModel:list){EnrollStudentEntityenrollStudentEntitynewEnrollStudentEntity();StudentEntitystudentEntitynewStudentEntity();BeanUtils。copyProperties(freshStudentAndStudentModel,studentEntity);BeanUtils。copyProperties(freshStudentAndStudentModel,enrollStudentEntity);StringoperatorTenancyContext。UserID。get();StringstudentIdBaseUuidUtils。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);AllusersEntityallusersEntitynewAllusersEntity();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);}enResultenrollStudentDao。insertAll(enrollStudentEntityList);stuResultstudentDao。insertAll(studentEntityList);allResultallusersFacade。insertUserList(allusersEntityList);if(enResult0stuResult0allResult){return10;}return10;}
  Mapper。xmlinsertidinsertAllparameterTypecom。dmsdbj。itoo。basicInfo。entity。EnrollStudentEntityinsertintotbenrollstudenttrimprefix(suffix)suffixOverrides,id,remark,nEMTaspiration,nEMTcode,nEMTscore,studentid,identitycardid,level,major,name,nation,secondarycollege,operator,sex,isdelete,accountaddress,nativeplace,originalplace,usedname,pictrue,joinpartydate,politicalstatus,telnum,isregistry,graduateschool,createtime,updatetimetrimvaluesforeachcollectionlistitemitemindexindexseparator,({item。id,jdbcTypeVARCHAR},{item。remark,jdbcTypeVARCHAR},{item。nemtAspiration,jdbcTypeVARCHAR},{item。nemtCode,jdbcTypeVARCHAR},{item。nemtScore,jdbcTypeVARCHAR},{item。studentId,jdbcTypeVARCHAR},{item。identityCardId,jdbcTypeVARCHAR},{item。level,jdbcTypeVARCHAR},{item。major,jdbcTypeVARCHAR},{item。name,jdbcTypeVARCHAR},{item。nation,jdbcTypeVARCHAR},{item。secondaryCollege,jdbcTypeVARCHAR},{item。operator,jdbcTypeVARCHAR},{item。sex,jdbcTypeVARCHAR},0,{item。accountAddress,jdbcTypeVARCHAR},{item。nativePlace,jdbcTypeVARCHAR},{item。originalPlace,jdbcTypeVARCHAR},{item。usedName,jdbcTypeVARCHAR},{item。pictrue,jdbcTypeVARCHAR},{item。joinPartyDate,jdbcTypeVARCHAR},{item。politicalStatus,jdbcTypeVARCHAR},{item。telNum,jdbcTypeVARCHAR},{item。isRegistry,jdbcTypeTINYINT},{item。graduateSchool,jdbcTypeVARCHAR},now(),now())foreachinsert
  代码说明:
  底层的mapper是通过逆向工程来生成的,批量插入如下,是拼接成类似:insertintotbenrollstudent()values(),()。();
  这样的缺点是,数据库一般有一个默认的设置,就是每次sql操作的数据不能超过4M。这样插入,数据多的时候,数据库会报错Packetforqueryistoolarge(60713934194304)。Youcanchangethisvalueontheserverbysettingthemaxallowedpacketvariable。,虽然我们可以通过
  类似修改my。ini加上maxallowedpacket67108864,6710886464M,默认大小4194304也就是4M
  修改完成之后要重启mysql服务,如果通过命令行修改就不用重启mysql服务。
  完成本次操作,但是我们不能保证项目单次最大的大小是多少,这样是有弊端的。所以可以考虑进行分组导入。三、分组把list导入Mysql中
  同样适用mybatis批量插入,区别是对每次的导入进行分组计算,然后分多次进行导入:Transactional(rollbackForException。class)publicintaddFreshStudentsNew2(ListFreshStudentAndStudentModellist,StringschoolNo){if(listnulllist。isEmpty()){return0;}ListStudentEntitystudentEntityListnewLinkedList();ListEnrollStudentEntityenrollStudentEntityListnewLinkedList();ListallusersEntityListnewLinkedList();for(FreshStudentAndStudentModelfreshStudentAndStudentModel:list){EnrollStudentEntityenrollStudentEntitynewEnrollStudentEntity();StudentEntitystudentEntitynewStudentEntity();BeanUtils。copyProperties(freshStudentAndStudentModel,studentEntity);BeanUtils。copyProperties(freshStudentAndStudentModel,enrollStudentEntity);StringoperatorTenancyContext。UserID。get();StringstudentIdBaseUuidUtils。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);AllusersEntityallusersEntitynewAllusersEntity();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);}intc100;intbenrollStudentEntityList。size()c;intdenrollStudentEntityList。size()c;intenResult0;intstuResult0;booleanallRfor(eec){enResultenrollStudentDao。insertAll(enrollStudentEntityList。subList(ec,e));stuResultstudentDao。insertAll(studentEntityList。subList(ec,e));allResultallusersFacade。insertUserList(allusersEntityList。subList(ec,e));}if(d!0){enResultenrollStudentDao。insertAll(enrollStudentEntityList。subList(cb,enrollStudentEntityList。size()));stuResultstudentDao。insertAll(studentEntityList。subList(cb,studentEntityList。size()));allResultallusersFacade。insertUserList(allusersEntityList。subList(cb,allusersEntityList。size()));}if(enResult0stuResult0allResult){return10;}return10;}
  代码说明:
  这样操作,可以避免上面的错误,但是分多次插入,无形中就增加了操作实践,很容易超时。所以这种方法还是不值得提倡的。
  再次改进,使用多线程分批导入。四、多线程分批导入Mysql
  依然使用mybatis的批量导入,不同的是,根据线程数目进行分组,然后再建立多线程池,进行导入。Transactional(rollbackForException。class)publicintaddFreshStudentsNew(ListFreshStudentAndStudentModellist,StringschoolNo){if(listnulllist。isEmpty()){return0;}ListStudentEntitystudentEntityListnewLinkedList();ListEnrollStudentEntityenrollStudentEntityListnewLinkedList();ListallusersEntityListnewLinkedList();list。forEach(freshStudentAndStudentModel{EnrollStudentEntityenrollStudentEntitynewEnrollStudentEntity();StudentEntitystudentEntitynewStudentEntity();BeanUtils。copyProperties(freshStudentAndStudentModel,studentEntity);BeanUtils。copyProperties(freshStudentAndStudentModel,enrollStudentEntity);StringoperatorTenancyContext。UserID。get();StringstudentIdBaseUuidUtils。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);AllusersEntityallusersEntitynewAllusersEntity();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);});intnThreads50;intsizeenrollStudentEntityList。size();ExecutorServiceexecutorServiceExecutors。newFixedThreadPool(nThreads);ListFutureIntegerfuturesnewArrayListFutureInteger(nThreads);for(inti0;inTi){finalListEnrollStudentEntityEnrollStudentEntityImputListenrollStudentEntityList。subList(sizenThreadsi,sizenThreads(i1));finalListStudentEntitystudentEntityImportListstudentEntityList。subList(sizenThreadsi,sizenThreads(i1));finalListallusersEntityImportListallusersEntityList。subList(sizenThreadsi,sizenThreads(i1));CallableIntegertask1(){studentSave。saveStudent(EnrollStudentEntityImputList,studentEntityImportList,allusersEntityImportList);return1;};futures。add(executorService。submit(task1));}executorService。shutdown();if(!futures。isEmpty()futures!null){return10;}return10;}
  代码说明:
  上面是通过应用ExecutorService建立了固定的线程数,然后根据线程数目进行分组,批量依次导入。一方面可以缓解数据库的压力,另一个面线程数目多了,一定程度会提高程序运行的时间。
  缺点就是要看服务器的配置,如果配置好的话就可以开多点线程,配置差的话就开小点。
  来源:blog。csdn。netkisscatforeverarticledetails79817039
投诉 评论

老美又不当人!台积电带去人才和产业链后,承诺补贴迟迟没有到位靠着在科技领域的领先,老美开始使用手中的权利,在全球范围内无恶不作,一旦有国家或企业在科技领域超过美国,就会遭到无端的打压。比如日本东芝,法国的阿尔斯通等,都遭到老美毒手……盘点S28赛季初辅助英雄硬辅时代回归,张飞盾山开启T0之旅hello大家好我是慕泽楠,每天更新峡谷不一样的资讯,关注我让你打开峡谷新世界!上期内容给大家分享了S28赛季的对抗路上分英雄推荐,希望通过每一期内容的分享可以帮助到大家……人间仙境风雪秘界那拉提摄影作品展(三)雪后的那拉提晴空如洗山峰、森林、山丘、草甸都被白雪覆盖勾勒出白色的线条大地更显苍茫辽阔《骏马》摄影张树杰在绵绵白雪装点下山丘,草原皓然……欧锦赛斯洛文尼亚8880德国东契奇36分德拉季奇18分施罗德直播吧9月7日讯在今日结束的男篮欧锦赛小组赛B组比赛中,斯洛文尼亚男篮8880战胜德国男篮。此役斯洛文尼亚方面,东契奇出战33分45秒,25传14中,三分8中2,罚球9中……在泰国,你只需要花费1000人民币,你能享受到的服务超乎你想泰国可以说是一个文化很独特的国家,加之风景也不错,近些年来尤其受到广大旅游爱好者的喜爱,尤其是我们中国人。(此处已添加小程序,请到今日头条客户端查看)每当假期来临,都有大……幸福的真谛是什么幸福的真谛是什么?幸福的起点来源于知足两个字,真正的知足是多也知足、少也知足、没有也知足。至美是不存在的,如果不能及早醒悟,便会付出一生的代价。世间没有完善的……新加坡青少年文化艺术中心主席胡刚把武术推向更广阔的世界舞台来源:人民日报海外版新加坡青少年文化艺术中心主席胡刚把武术推向更广阔的世界舞台(侨胞说祖国在我心中(82))春节刚过,新加坡青少年文化艺术中心主席胡刚就迫不及……李一桐差在哪儿?娱评大赏刷到李一桐毕雯珺同游迪士尼,那撒糖阵仗还以为公开恋情了,结果是为了宣传新剧《九霄寒夜暖》,但剧的热度还不如他俩营业来得高。再往前,《狂飙》领着全剧组飞升,女演员中……给你2万条数据,怎么快速导入到MySQL?写得太好了一、前言前两天做了一个导入的功能,导入开始的时候非常慢,导入2w条数据要1分多钟,后来一点一点的优化,从直接把list怼进Mysql中,到分配把list导入Mysql中,……中国票房最高的10部儿童亲子电影,熊出没系列仅一部入围以前的儿童节,是游乐场和玩具的天下;现在的儿童节,多了电影。随着中国电影市场的高速发展,影院上是有着越来越多适合小孩子观看的亲子向电影。而且,亲子市场的票房在……再见,我的2022!2022年马上擦肩而过,挥手告别,再也没有交集。想起2021年末盘点时,我满心欢喜地写下《我想要的未来,她正向我走来》。如今一年过去,现实是:我想要的未来,她转了一个很大……王者荣耀新手怎么双排上分?看完这个组合就起飞王者荣耀这款游戏是大家耳熟能详的,因此在游戏中上分就成为大家津津乐道的一个话题了。今天给大家推荐双排或者cp新手上分的发育路英雄组合,后羿王昭君辅助。首先是英雄获取的容易……
仙女下凡!齐溪浪姐上班照曝光,白色小吊带秀身姿,肚脐眼都好看杜兰特换布朗加盟凯尔特人用户信息被窃取,蔚来需要担责吗?床上除螨虫的简单方法小小螨虫令人唯恐避之不及大人被螨虫咬了的图片被咬5大症状和灭螨虫5大招人感染螨虫的皮肤图片有螨虫皮肤是什么样的螨虫咬过后的疙瘩照片一个人对螨虫过敏了怎么办卧室床上螨虫咬的症状图片螨虫的危害及去除方法Linux配置网络yum源的方法,很重要丧尸小说排行榜前十名想惊心刺激就看丧尸小说这4款骁龙8Gen1手机,跑分都超过100万,真正的国货之光手指起水泡是怎么回事手指上有小水泡很痒和不痒的区别

友情链接:中准网聚热点快百科快传网快生活快软网快好知文好找美丽时装彩妆资讯历史明星乐活安卓数码常识驾车健康苹果问答网络发型电视车载室内电影游戏科学音乐整形