JPA的saveAll方法的效率居然高于 jdbcTemplate.batchUpdate方法

 @DS(DataSourceTypeEnum.amdb_collect)
        @Override
        public void dropAndSave(List<PsInfoCollect> psInfoCollects){
            psMonitorInfoCollectRepository.deleteAllItem();
            long a=System.currentTimeMillis();
            psMonitorInfoCollectRepository.saveAll(psInfoCollects);
          //  batchInsert(psInfoCollects);
            long b = System.currentTimeMillis();
            logger.error("批量插入用时:" + (b-a) +"毫秒");
        }

 当我使用jpa的saveAll方法时,居然发现这里的插入是一条一条的插入(插入900多条数据,此时可以看到大概是需要2秒钟)。然后就想到用jdbc的批量插入方法。于是使用

jdbcTemplate.batchUpdate方法。
 @DS(DataSourceTypeEnum.amdb_collect)
        @Override
        public void dropAndSave(List<PsInfoCollect> psInfoCollects){
            psMonitorInfoCollectRepository.deleteAllItem();
            long a=System.currentTimeMillis();
           // psMonitorInfoCollectRepository.saveAll(psInfoCollects);
            batchInsert(psInfoCollects);
            long b = System.currentTimeMillis();
            logger.error("批量插入用时:" + (b-a) +"毫秒");
        }

    private void batchInsert(List<PsInfoCollect> psInfoCollects) {
        String sql = "insert  " +
                "    into " +
                "        psinfo_collect " +
                "        (apply_range, ps_id, mp_id, pollutant_code, city_name, credit_code, daily_avg_standard, dqyl, effective_time, environment_principal, equip_name, exmax, exmin, guilv, high, industry_name, is_cdpf, is_important_mp, is_important_pol, is_important_ps, is_zs, jianguan_industry, latitude, longitude, maintance_unit, manufacture_date, manufacturer, mn, montiortype, mp_name, mp_status, mp_type, mp_type_name, outlet_name, outlet_status, outlet_yq, outlut_standard, phone, pol_name, pol_status, position, position_mp, province_name, ps_address, ps_name, ps_status, pwxk_bh, pwxk_bh_pk, pwxk_industry, quxian_name, quxiang, region_code, sampling_area, standard_code, standard_industry, standard_level, standard_name, standard_type, summer_begin_time, update_time, valley, winter_bengin_time, winter_outout_standard, year_limit, zhijing, caiyangzhouqi, cdpf_outlut_standard, area_code)  " +
                "    values " +
                "        (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";



        List<Object[]> list = new ArrayList<>();
        for (PsInfoCollect psInfoCollect:psInfoCollects) {
            list.add(new Object[]{psInfoCollect.getApplyRange(),psInfoCollect.getPsId(),psInfoCollect.getMpId(),psInfoCollect.getPollutantCode(),psInfoCollect.getCityName(),psInfoCollect.getCreditCode(),psInfoCollect.getDailyAvgStandard(),psInfoCollect.getDqyl(),psInfoCollect.getEffectiveTime(),psInfoCollect.getEnvironmentPrincipal(),psInfoCollect.getEquipName(),psInfoCollect.getExmax(),psInfoCollect.getExmin(),psInfoCollect.getGuilv(),psInfoCollect.getHigh(),psInfoCollect.getIndustryName(),psInfoCollect.getIsCdpf(),psInfoCollect.getIsImportantMp(),psInfoCollect.getIsImportantPol(),psInfoCollect.getIsImportantPs(),psInfoCollect.getIsZs(),psInfoCollect.getJianguanIndustry(),psInfoCollect.getLatitude(),psInfoCollect.getLongitude(),psInfoCollect.getMaintanceUnit(),psInfoCollect.getManufactureDate(),psInfoCollect.getManufacturer(),psInfoCollect.getMn(),psInfoCollect.getMontiortype(),psInfoCollect.getMpName(),psInfoCollect.getMpStatus(),psInfoCollect.getMpType(),psInfoCollect.getMpTypeName(),psInfoCollect.getOutletName(),psInfoCollect.getOutletStatus(),psInfoCollect.getOutletYq(),psInfoCollect.getOutlutStandard(),psInfoCollect.getPhone(),psInfoCollect.getPolName(),psInfoCollect.getPolStatus(),psInfoCollect.getPosition(),psInfoCollect.getPositionMp(),psInfoCollect.getProvinceName(),psInfoCollect.getPsAddress(),psInfoCollect.getPsName(),psInfoCollect.getPsStatus(),psInfoCollect.getPwxkBh(),psInfoCollect.getPwxkBhPk(),psInfoCollect.getPwxkIndustry(),psInfoCollect.getQuxianName(),psInfoCollect.getQuxiang(),psInfoCollect.getRegionCode(),psInfoCollect.getSamplingArea(),psInfoCollect.getStandardCode(),psInfoCollect.getStandardIndustry(),psInfoCollect.getStandardLevel(),psInfoCollect.getStandardName(),psInfoCollect.getStandardType(),psInfoCollect.getSummerBeginTime(),psInfoCollect.getUpdateTime(),psInfoCollect.getValley(),psInfoCollect.getWinterBenginTime(),psInfoCollect.getWinterOutoutStandard(),psInfoCollect.getYearLimit(),psInfoCollect.getZhijing(),psInfoCollect.getCaiyangzhouqi(),psInfoCollect.getCdpfOutlutStandard(),psInfoCollect.getAreaCode()});

        }

        jdbcTemplate.batchUpdate(sql, list);

    }

 执行完后发现

使用jdbc的批量查询居然需要16秒左右,远远超过了jpa的saveAll方法。

百度之后jdbcTemplate.batchUpdate方法。居然也是一条一条的插入。其中有一个解决方法是

在连接数据库的URL上加上参数rewriteBatchedStatements=true。但是此方法只对数据库是mysql有效。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值