数据库同时插入多条数据,提升性能

同时向数据库插入数据,List<Waypoints> waypoints插入的是waypoints列表,里面有300多条内容,这样一次次重复插入,太慢了,要等半分钟多,

这是原先的代码

1.航线列表Service业务层处理

    @Override
    public int insertFlightPlansAndWaypoints(FlightPlans newflightPlans, List<Waypoints> waypoints) {
        newflightPlans.setCreateTime(DateUtils.getNowDate());
        int rows = flightPlansMapper.insertFlightPlans(newflightPlans);
        
        //获取航线id
        String fpId = newflightPlans.getFpId();
        long wpIndex = 0;
        for (Waypoints waypoint : waypoints) {
            waypoint.setWpFlightPlanId(fpId);
            waypoint.setWpIndex(wpIndex++); // 设置 wp_index,并在每次循环中加 1
            waypointsService.insertWaypoints(waypoint);
        }
        return rows;
    }

2.航线对应坐标列Mapper接口

public int insertWaypoints(Waypoints waypoints);

3.xml

 <insert id="insertWaypoints" parameterType="Waypoints" useGeneratedKeys="true"
            keyProperty="wpId">
        insert into waypoints
        <trim prefix="(" suffix=")" suffixOverrides=",">
                    <if test="wpFlightPlanId != null and wpFlightPlanId != ''">`wp_flight_plan_id`,
                    </if>
                    <if test="wpIndex != null">`wp_index`,
                    </if>
                    <if test="wpLongitude != null">`wp_longitude`,
                    </if>
                    <if test="wpLatitude != null">`wp_latitude`,
                    </if>
                    <if test="wpAltitude != null">`wp_altitude`,
                    </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
                    <if test="wpFlightPlanId != null and wpFlightPlanId != ''">#{wpFlightPlanId},
                    </if>
                    <if test="wpIndex != null">#{wpIndex},
                    </if>
                    <if test="wpLongitude != null">#{wpLongitude},
                    </if>
                    <if test="wpLatitude != null">#{wpLatitude},
                    </if>
                    <if test="wpAltitude != null">#{wpAltitude},
                    </if>
        </trim>
    </insert>

采用批量插入(Batch Insert)来提高性能。

1.首先在 Waypoints 的 Mapper 接口中添加批量插入方法:

/**
     * 批量新增航线对应坐标列
     *
     * @param waypoints 航线对应坐标列
     * @return 结果
     */
    int batchInsertWaypoints(List<Waypoints> waypoints);

2.在对应的 Mapper XML 文件中添加批量插入的 SQL:

    <!-- 添加新的批量插入方法 -->
    <insert id="batchInsertWaypoints" parameterType="java.util.List">
        insert into waypoints
        (`wp_flight_plan_id`, `wp_index`, `wp_longitude`, `wp_latitude`, `wp_altitude`)
        values
        <foreach collection="list" item="item" separator=",">
            (
            #{item.wpFlightPlanId},
            #{item.wpIndex},
            #{item.wpLongitude},
            #{item.wpLatitude},
            #{item.wpAltitude}
            )
        </foreach>
    </insert>

3.修改服务方法:

  @Override
    public int insertFlightPlansAndWaypoints(FlightPlans newflightPlans, List<Waypoints> waypoints) {
        newflightPlans.setCreateTime(DateUtils.getNowDate());
        int rows = flightPlansMapper.insertFlightPlans(newflightPlans);

        // 获取航线id
        String fpId = newflightPlans.getFpId();

        // 批量设置 wpFlightPlanId 和 wpIndex
        for (int i = 0; i < waypoints.size(); i++) {
            Waypoints waypoint = waypoints.get(i);
            waypoint.setWpFlightPlanId(fpId);
            waypoint.setWpIndex((long) i);
        }

        // 批量插入航点数据
        waypointsMapper.batchInsertWaypoints(waypoints);

        return rows;
    }

如果数据量大,建议分批处理

@Override
public int insertFlightPlansAndWaypoints(FlightPlans newflightPlans, List<Waypoints> waypoints) {
    newflightPlans.setCreateTime(DateUtils.getNowDate());
    int rows = flightPlansMapper.insertFlightPlans(newflightPlans);

    // 获取航线id
    String fpId = newflightPlans.getFpId();
    
    // 批量设置航点数据
    for (int i = 0; i < waypoints.size(); i++) {
        Waypoints waypoint = waypoints.get(i);
        waypoint.setWpFlightPlanId(fpId);
        waypoint.setWpIndex((long) i);
    }
    
    // 如果数据量大,建议分批处理
    final int BATCH_SIZE = 500;
    for (int i = 0; i < waypoints.size(); i += BATCH_SIZE) {
        int end = Math.min(i + BATCH_SIZE, waypoints.size());
        List<Waypoints> batch = waypoints.subList(i, end);
        waypointsMapper.batchInsertWaypoints(batch);
    }
    
    return rows;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值