redis 中缓存 百万级别表的查询数据 出错:Query execution was interrupted

项目背景:项目需要 首检合格率 这个结果,但是sql执行非常慢,就想着使用redis来优化接口的速度。现在我需要将数据库查询结果存储到redis,但是就是这一小步,也非常困难。我是用定时任务来实现上面的目的。

复制代码
@Component
@Slf4j
public class FirstInspectionPassRateTask {
    @Autowired
    private CheckResultMapper checkResultMapper;

    @Autowired
    private InspectionStationInfoMapper inspectionStationInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Scheduled(cron = "0 32 13 * * ?")
    public void calculateFirstInspectionPassRate(){
        log.info("####### 定时任务开启......");
        ValueOperations<String ,String> operations = redisTemplate.opsForValue();

        NumberFormat numberFormat = NumberFormat.getPercentInstance();
        numberFormat.setMinimumFractionDigits(2);

        List<String> unitIdList = inspectionStationInfoMapper.selectUnitId();//单位id 集合
        unitIdList = Optional.ofNullable(unitIdList).orElse(new ArrayList<>());
        //这里只给2024表中的统计数据,因为首检合格率需要跨表,目前我也想不出如何编写 TODO
        List<String> monthList = new ArrayList<>(Arrays.asList("03"));

        unitIdList.forEach(unitId -> {
            monthList.forEach(month -> {
                List<String> bgList = new ArrayList<>();
                //查询指定机构的 首检报告列表
                bgList = checkResultMapper.getFirstInspectionReportCount(unitId, "2024", month);
                bgList = Optional.ofNullable(bgList).orElse(new ArrayList<>());
                //计算指定机构的 首检报告数量
                Long bgSum = (long) bgList.size();

                //首检合格率
                String rate = "0.00%";
                if(bgSum != 0){
                    //计算异常的首检报告数量
                    Long warnBgSum = checkResultMapper.getWarnFirstInspectionReportCount(unitId,"2024",month,bgList);
                    rate = numberFormat.format((bgSum-warnBgSum)/bgSum.doubleValue());
                }
                rate = rate.substring(0,rate.length() -1 );//去除最后的 % 方便前端处理

                String key = String.format("SZJDC:CHECKRESULT:BGBH:%s",unitId+"-2024-"+month);
                operations.set(key,rate,20, TimeUnit.DAYS);
            });
        });
    }

}

就在执行这个定时任务,我出现了非常多的报错:Query execution was interrupted

复制代码
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@6eaf527b]
10:45:18.655 [pool-2-thread-1] ERROR o.s.s.s.TaskUtils$LoggingErrorHandler - Unexpected error occurred in scheduled task
org.springframework.dao.QueryTimeoutException:
### Error querying database. Cause: java.sql.SQLTimeoutException: Query execution was interrupted
### The error may exist in file [D:\workspace\IdeaProjects\szjdc-service\sz-warning-platform\target\classes\mappers\CheckResultMapper.xml]
### The error may involve com.iflytek.warning.mapper.CheckResultMapper.getFirstInspectionReportCount
### The error occurred while handling results
### SQL: select BGBH from check_result_2024 cr WHERE year(CHECK_DATE) = ? and month(CHECK_DATE) = ? and UNIT_ID = ? and NOT EXISTS ( select CHECK_ID from check_result_2024 pcr WHERE pcr.CHECK_DATE between DATE_SUB(cr.CHECK_DATE, INTERVAL 4 MONTH) and DATE_SUB(cr.CHECK_DATE , INTERVAL 1 DAY) and cr.VIN = pcr.VIN and UNIT_ID = ? )
### Cause: java.sql.SQLTimeoutException: Query execution was interrupted

这里给出我的解决办法:

这上面处理完了之后还是出现了该报错,然后我请教前辈

就解决了,不愧是前辈

相关推荐
workflower3 分钟前
MDSE和敏捷开发相互矛盾之处:方法论本质的冲突
数据库·软件工程·敏捷流程·极限编程
Tony小周13 分钟前
实现一个点击输入框可以弹出的数字软键盘控件 qt 5.12
开发语言·数据库·qt
lifallen31 分钟前
Paimon 原子提交实现
java·大数据·数据结构·数据库·后端·算法
TDengine (老段)1 小时前
TDengine 数据库建模最佳实践
大数据·数据库·物联网·时序数据库·tdengine·涛思数据
Elastic 中国社区官方博客1 小时前
Elasticsearch 字符串包含子字符串:高级查询技巧
大数据·数据库·elasticsearch·搜索引擎·全文检索·lucene
Gauss松鼠会2 小时前
GaussDB应用场景全景解析:从金融核心到物联网的分布式数据库实践
数据库·分布式·物联网·金融·database·gaussdb
天河归来2 小时前
springboot框架redis开启管道批量写入数据
java·spring boot·redis
守城小轩2 小时前
Chromium 136 编译指南 - Android 篇:开发工具安装(三)
android·数据库·redis
尽兴-2 小时前
如何将多个.sql文件合并成一个:Windows和Linux/Mac详细指南
linux·数据库·windows·sql·macos
小小不董2 小时前
深入理解oracle ADG和RAC
linux·服务器·数据库·oracle·dba