sql优化杂记——回表

			String end = DateUtil.getDate(DateUtil.YYYY_MM_DD_HH_MM_SS);
            String begin = DateUtil.getBeforeDay(new Date(),DateUtil.YYYY_MM_DD_HH_MM_SS);
            log.info("存管回调报文重试任务开始begin:{},end:{}",begin,end);
            BatchCallbackRetryRecord first = batchCallbackRetryRecordMapper.getFirstByCreateTime(begin,end);
            if(first == null){
                log.info("无重试订单,直接返回");
                return new ReturnT<String>(200, OrderStatusEnum.SUCCESS.getCode());
            }
            BatchCallbackRetryRecord last = batchCallbackRetryRecordMapper.getLastByCreateTime(begin,end);

            Long firstId = first.getId();
            Long lastId = last.getId();
            log.info("存管回调报文重试,firstId:{},lastId:{}",firstId,lastId);
            Semaphore semaphore = new Semaphore(concurrency);
            if(firstId.equals(lastId)){
                jobHandlerService.depositoryCallback(first,semaphore);
            }
            while(firstId < lastId){
                List<BatchCallbackRetryRecord> list = batchCallbackRetryRecordMapper.selectById(firstId,firstId+count);
                if(list == null || list.size() == 0){
                    firstId = firstId + count;
                    continue;
                }
                for(BatchCallbackRetryRecord retryRecord : list){
                    if(retryRecord.getRetryTimes() >= notifyRetryTime){
                        continue;
                    }
                    jobHandlerService.depositoryCallback(retryRecord,semaphore);
                }
                firstId = firstId + count;
            }


            //查看当前可用许可数,等于concurrency结束
            while (semaphore.availablePermits() != concurrency){
                Thread.sleep(1000);
            }
	@Select({
            "select * from tb_batch_callback_retry_record where status = 'FAIL' and create_time > #{begin,jdbcType=VARCHAR} and create_time <= #{end,jdbcType=VARCHAR} limit 1"
    })
    @ResultMap("BaseResultMap")
    BatchCallbackRetryRecord getFirstByCreateTime(@Param("begin")String begin,@Param("end")String end);

	@Select({
            "select * from tb_batch_callback_retry_record where status = 'FAIL' and create_time > #{begin,jdbcType=VARCHAR} and create_time <= #{end,jdbcType=VARCHAR} order by id desc limit 1"
    })
    @ResultMap("BaseResultMap")
    BatchCallbackRetryRecord getLastByCreateTime(@Param("begin")String begin,@Param("end")String end);


@Select({
            "select * from tb_batch_callback_retry_record where status = 'FAIL' and id >= #{first,jdbcType=INTEGER} and id <= #{last,jdbcType=INTEGER}"
    })
    @ResultMap("BaseResultMap")
    List<BatchCallbackRetryRecord> selectById(@Param("first")long first,@Param("last")long last);

create_time字段没有索引,所以需要通过create_time找到第一条数据和最后一条数据。
再通过上面两个数据的id获取中间范围的数据集合,通过id找效率比较高。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值