性能優(yōu)化之Java多線程批量拆分List導(dǎo)入數(shù)據(jù)庫
| 前言
前兩天做了一個(gè)導(dǎo)入的功能,導(dǎo)入開始的時(shí)候非常慢,導(dǎo)入2w條數(shù)據(jù)要1分多鐘,后來一點(diǎn)一點(diǎn)的優(yōu)化,從直接把list懟進(jìn)Mysql中,到分配把list導(dǎo)入Mysql中,到多線程把list導(dǎo)入Mysql中。時(shí)間是一點(diǎn)一點(diǎn)的變少了。非常的爽,最后變成了10s以內(nèi)。下面就展示一下過程。
| 直接把list懟進(jìn)Mysql
使用mybatis的批量導(dǎo)入操作:??@Transactional(rollbackFor?=?Exception.class)
????public?int?addFreshStudentsNew2(List<FreshStudentAndStudentModel>?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());??//密碼設(shè)置為考生號(hào)
????????????allusersEntityList.add(allusersEntity);
????????}
????????????enResult?=?enrollStudentDao.insertAll(enrollStudentEntityList);
????????????stuResult?=?studentDao.insertAll(studentEntityList);
????????????allResult?=?allusersFacade.insertUserList(allusersEntityList);
????????if?(enResult?>?0?