Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/57.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JDBC多行插入性能非常糟糕_Java_Mysql_Spring_Spring Jdbc_Rds - Fatal编程技术网

Java JDBC多行插入性能非常糟糕

Java JDBC多行插入性能非常糟糕,java,mysql,spring,spring-jdbc,rds,Java,Mysql,Spring,Spring Jdbc,Rds,为什么我会在这个Spring JDBC实现中增加批处理大小时获得线性性能。除了解释、指示检查和数据库设置调优之外,我还可能做一些进一步的调优吗?我应该提到,我是在AWSdb.r3.2xlargeclassRDS服务器上托管的MySQL 5.6实例上运行这段代码的 @Repository public class JdbcRatePlanLevelCostPriceLogRepository implements Insertable<RatePlanLevelCostPriceLog>

为什么我会在这个Spring JDBC实现中增加批处理大小时获得线性性能。除了解释、指示检查和数据库设置调优之外,我还可能做一些进一步的调优吗?我应该提到,我是在AWS
db.r3.2xlarge
classRDS服务器上托管的
MySQL 5.6
实例上运行这段代码的

@Repository
public class JdbcRatePlanLevelCostPriceLogRepository implements Insertable<RatePlanLevelCostPriceLog> {

@Autowired
JdbcTemplate jdbcTemplate;

private static final String INSERT_STATEMENT = InsertStatementBuilder.build();

@Override
public RatePlanLevelCostPriceLog insert(RatePlanLevelCostPriceLog entity) {
    jdbcTemplate.execute(INSERT_STATEMENT, new SingleCallbackImpl(entity));
    return entity;
}

@Override
public List<RatePlanLevelCostPriceLog> insert(List<RatePlanLevelCostPriceLog> entities) {
    jdbcTemplate.batchUpdate(INSERT_STATEMENT, new BatchPreparedStatementSetter() {

        @Override
        public void setValues(PreparedStatement ps, int i) throws SQLException {
            RatePlanLevelCostPriceLog entity = entities.get(i);
            PreparedStatementTranslator.translate(ps, entity);
        }

        @Override
        public int getBatchSize() {
            return entities.size();
        }
    });
    return entities;
}

private static class InsertStatementBuilder {

    static String build() {
        StringBuffer colsBuffer = new StringBuffer();
        StringBuffer valuesBuffer = new StringBuffer();
        colsBuffer.append("INSERT INTO rate_plan_level_cost_price_log(");
        valuesBuffer.append("VALUES (");
        // handle the @NotNull fields
        colsBuffer.append("hotel_id");
        colsBuffer.append(",rate_plan_id");
        colsBuffer.append(",stay_date");
        colsBuffer.append(",rate_plan_level");
        colsBuffer.append(",person_count");
        colsBuffer.append(",length_of_stay_in_days");
        colsBuffer.append(",rpcp_log_seq_num");
        colsBuffer.append(",log_action_type_id");
        colsBuffer.append(",active_status_type_id");
        colsBuffer.append(",supplier_update_date");
        colsBuffer.append(",create_date");
        colsBuffer.append(",supplier_update_tpid");
        colsBuffer.append(",supplier_update_tuid");
        colsBuffer.append(",supplier_log_seq_num");
        // handle the optional fields
        colsBuffer.append(",cost_amount");
        colsBuffer.append(",cost_code");
        colsBuffer.append(",price_amount");
        colsBuffer.append(",change_request_id");
        colsBuffer.append(",change_request_id_old");
        colsBuffer.append(",lar_amount");
        colsBuffer.append(",lar_margin_amount");
        colsBuffer.append(",lar_taxes_and_fees_amount");
        valuesBuffer.append("?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?");
        colsBuffer.append(")");
        valuesBuffer.append(")");
        return String.join(" ", colsBuffer.toString(), valuesBuffer.toString());
    }
}

private static class PreparedStatementTranslator {

    static void translate(PreparedStatement ps, RatePlanLevelCostPriceLog entity) throws SQLException {
        int i = 0;
        // handle the @NotNull fields
        ps.setInt(++i, entity.getHotelId());
        ps.setInt(++i, entity.getRatePlanId());
        ps.setTimestamp(++i, new Timestamp(entity.getStayDate().getTime()));
        ps.setInt(++i, entity.getRatePlanLevel());
        ps.setInt(++i, entity.getPersonCount());
        ps.setInt(++i, entity.getLengthOfStayInDays());
        ps.setInt(++i, entity.getRpcpLogSeqNum());
        ps.setInt(++i, entity.getLogActionTypeId());
        ps.setInt(++i, entity.getActiveStatusTypeId());
        ps.setTimestamp(++i, new Timestamp(entity.getSupplierUpdateDate().getTime()));
        ps.setTimestamp(++i, new Timestamp(entity.getCreateDate().getTime()));
        ps.setInt(++i, entity.getSupplierUpdateTpid());
        ps.setInt(++i, entity.getSupplierUpdateTuid());
        ps.setInt(++i, entity.getSupplierLogSeqNum());
        // handle the optional fields
        if (entity.getCostAmount() != null) {
            ps.setDouble(++i, entity.getCostAmount());
        } else {
            ps.setNull(++i, Types.DOUBLE);
        }
        if (entity.getCostCode() != null) {
            ps.setString(++i, entity.getCostCode());
        } else {
            ps.setNull(++i, Types.VARCHAR);
        }
        if (entity.getPriceAmount() != null) {
            ps.setDouble(++i, entity.getPriceAmount());
        } else {
            ps.setNull(++i, Types.DOUBLE);
        }
        if (entity.getChangeRequestId() != null) {
            ps.setInt(++i, entity.getChangeRequestId());
        } else {
            ps.setNull(++i, Types.INTEGER);
        }
        if (entity.getChangeRequestIdOld() != null) {
            ps.setInt(++i, entity.getChangeRequestIdOld());
        } else {
            ps.setNull(++i, Types.INTEGER);
        }
        if (entity.getLarAmount() != null) {
            ps.setDouble(++i, entity.getLarAmount());
        } else {
            ps.setNull(++i, Types.DOUBLE);
        }
        if (entity.getLarMarginAmount() != null) {
            ps.setDouble(++i, entity.getLarMarginAmount());
        } else {
            ps.setNull(++i, Types.DOUBLE);
        }
        if (entity.getLarTaxesAndFeesAmount() != null) {
            ps.setDouble(++i, entity.getLarTaxesAndFeesAmount());
        } else {
            ps.setNull(++i, Types.DOUBLE);
        }
    }

}

private class SingleCallbackImpl implements PreparedStatementCallback<Boolean> {

    private final RatePlanLevelCostPriceLog entity;

    SingleCallbackImpl(RatePlanLevelCostPriceLog entity) {
        this.entity = entity;
    }

    @Override
    public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException,
                    DataAccessException {
        PreparedStatementTranslator.translate(ps, entity);
        return ps.execute();
    }

}

}
@存储库
公共类JdbcRatePlanLevelCostPriceLogRepository实现可插入{
@自动连线
jdbc模板jdbc模板;
私有静态最终字符串INSERT_STATEMENT=InsertStatementBuilder.build();
@凌驾
公共RatePlanLevelCostPriceLog插入(RatePlanLevelCostPriceLog实体){
execute(INSERT_语句,新的SingleCallbackImpl(entity));
返回实体;
}
@凌驾
公共列表插入(列表实体){
batchUpdate(插入_语句,新的BatchPreparedStatementSetter(){
@凌驾
公共void setValues(PreparedStatement ps,int i)引发SQLException{
RatePlanLevelCostPriceLog entity=entities.get(i);
PreparedStatementTranslator.translate(ps,实体);
}
@凌驾
public int getBatchSize(){
返回实体的大小();
}
});
返回实体;
}
私有静态类InsertStatementBuilder{
静态字符串构建(){
StringBuffer colsBuffer=新的StringBuffer();
StringBuffer值Buffer=新StringBuffer();
colsBuffer.append(“插入到费率、计划、级别、成本、价格、日志中(”);
valuesBuffer.append(“值(”);
//处理@NotNull字段
colsBuffer.append(“酒店id”);
colsBuffer.append(“,rate\u plan\u id”);
colsBuffer.append(“,停留日期”);
colsBuffer.append(“,费率计划水平”);
colsBuffer.append(“,person_count”);
colsBuffer.append(“,停留时间(以天为单位”);
colsBuffer.append(“,rpcp_log_seq_num”);
colsBuffer.append(“,log\u action\u type\u id”);
colsBuffer.append(“,active_status_type_id”);
colsBuffer.append(“,供应商更新日期”);
colsBuffer.append(“,create_date”);
colsBuffer.append(“,供应商更新\u tpid”);
colsBuffer.append(“,供应商更新号”);
colsBuffer.append(“,供应商日志序号”);
//处理可选字段
colsBuffer.追加(“成本金额”);
colsBuffer.append(“,成本代码”);
colsBuffer.追加(“,价格/金额”);
colsBuffer.append(“,change\u request\u id”);
colsBuffer.append(“,change\u request\u id\u old”);
colsBuffer.追加(“,lar_金额”);
colsBuffer.追加(“lar_保证金金额”);
colsBuffer.追加(“,lar_税和费用金额”);
附加(“?,?,?,?,,?,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,?”;
colsBuffer.append(“)”;
值缓冲。附加(“)”;
返回字符串.join(“,colsBuffer.toString(),valuesBuffer.toString());
}
}
私有静态类PreparedStatementTranslator{
静态void translate(PreparedStatement ps,RatePlanLevelCostPriceLog实体)引发SQLException{
int i=0;
//处理@NotNull字段
ps.setInt(++i,entity.getHotelId());
ps.setInt(++i,entity.getRatePlanId());
ps.setTimestamp(++i,新的时间戳(entity.getStayDate().getTime());
ps.setInt(++i,entity.getRatePlanLevel());
ps.setInt(++i,entity.getPersonCount());
ps.setInt(++i,entity.getLengthOfStayInDays());
ps.setInt(++i,entity.getRpcpLogSeqNum());
ps.setInt(++i,entity.getLogActionTypeId());
ps.setInt(++i,entity.getActiveStatusTypeId());
ps.setTimestamp(++i,新时间戳(entity.getSupplierUpdateDate().getTime());
ps.setTimestamp(++i,新的时间戳(entity.getCreateDate().getTime());
ps.setInt(++i,entity.getSupplierUpdatePID());
ps.setInt(++i,entity.getSupplierUpdateUID());
ps.setInt(++i,entity.getSupplierLogSeqNum());
//处理可选字段
if(entity.getCostAmount()!=null){
ps.setDouble(++i,entity.getCostAmount());
}否则{
ps.setNull(++i,Types.DOUBLE);
}
if(entity.getCostCode()!=null){
ps.setString(++i,entity.getCostCode());
}否则{
ps.setNull(++i,Types.VARCHAR);
}
if(entity.getPriceAmount()!=null){
ps.setDouble(++i,entity.getPriceAmount());
}否则{
ps.setNull(++i,Types.DOUBLE);
}
if(entity.getChangeRequestId()!=null){
ps.setInt(++i,entity.getChangeRequestId());
}否则{
ps.setNull(++i,Types.INTEGER);
}
if(entity.getChangeRequestIdOld()!=null){
ps.setInt(++i,entity.getChangeRequestIdOld());
}否则{
ps.setNull(++i,Types.INTEGER);
}
if(entity.getLarAmount()!=null){
ps.setDouble(++i,entity.getLarAmount());
}否则{
ps.setNull(++i,Types.DOUBLE);
}
if(entity.getLarMarginAmount()!=null){
ps.setDouble(++i,entity.getLarMarginAmount());
}否则{
ps.setNull(++i,Types.DOUBLE);
}
if(entity.getLarTaxesAndFeesAmount()!=null){
ps.setDouble(++i,entity.getLarTaxesAndFeesAmount());
}否则{
ps.setNull(++i,Types.DOUBLE);
}
}
}
私有类SingleCallbackImpl实现PreparedStatementCallback{
私人最终费率计划级别成本价格记录实体;
SingleCallbackImpl(RatePlanLevelCostPriceLog实体){
this.entity=实体;
}
@凌驾
公共布尔doInPreparedStatement(PreparedStatement ps)引发SQLException,
DataAccessException{
PreparedStatementTranslator.translate(ps,实体);
返回ps.execute();
}
}
}
执行单个记录插入需要
~250 ms
,然后