java多线程批量拆分List导入数据库的实现过程
一、前言
前两天做了一个导入的功能,导入开始的时候非常慢,导入2w条数据要1分多钟,后来一点一点的优化,从直接把list怼进mysql中,到分配把list导入mysql中,到多线程把list导入mysql中。时间是一点一点的变少了。非常的爽,最后变成了10s以内。下面就展示一下过程。
二、直接把list怼进mysql
使用mybatis的批量导入操作:
@transactional(rollbackfor = exception.class) public int addfreshstudentsnew2(list<freshstudentandstudentmodel> list, string schoolno) { if (list == null || list.isempty()) { return 0; } list<studententity> studententitylist = new linkedlist<>(); list<enrollstudententity> enrollstudententitylist = new linkedlist<>(); list<allusersentity> 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 id="insertall" parametertype="com.dmsdbj.itoo.basicinfo.entity.enrollstudententity"> insert into tb_enroll_student <trim prefix="(" suffix=")" suffixoverrides=","> 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 </trim> values <foreach collection="list" item="item" index="index" separator=","> ( #{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() ) </foreach> </insert>
代码说明:
底层的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<freshstudentandstudentmodel> list, string schoolno) { if (list == null || list.isempty()) { return 0; } list<studententity> studententitylist = new linkedlist<>(); list<enrollstudententity> enrollstudententitylist = new linkedlist<>(); list<allusersentity> 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<freshstudentandstudentmodel> list, string schoolno) { if (list == null || list.isempty()) { return 0; } list<studententity> studententitylist = new linkedlist<>(); list<enrollstudententity> enrollstudententitylist = new linkedlist<>(); list<allusersentity> 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<future<integer>> futures = new arraylist<future<integer>>(nthreads); for (int i = 0; i < nthreads; i++) { final list<enrollstudententity> enrollstudententityimputlist = enrollstudententitylist.sublist(size / nthreads * i, size / nthreads * (i + 1)); final list<studententity> studententityimportlist = studententitylist.sublist(size / nthreads * i, size / nthreads * (i + 1)); final list<allusersentity> allusersentityimportlist = allusersentitylist.sublist(size / nthreads * i, size / nthreads * (i + 1)); callable<integer> 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 建立了固定的线程数,然后根据线程数目进行分组,批量依次导入。一方面可以缓解数据库的压力,另一个面线程数目多了,一定程度会提高程序运行的时间。缺点就是要看服务器的配置,如果配置好的话就可以开多点线程,配置差的话就开小点。
五、小结
通过使用这个操作真是不断的提高了,项目使用技巧也是不错。加油~~ 多线程哦~~
到此这篇关于java多线程批量拆分list导入数据库的文章就介绍到这了,更多相关java多线程批量拆分list内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!