因
每晚12点20分定时任务做数据统计,mongodb 50万条数据开始,每天晚上CPU报警:CPU>=95,并耗时3分钟以上.
bash
2023-12-08 00:20:00.023 [Thread-95] INFO c.q.i.q.jobhandler.dataMongoDBXxlJob - ==========定时生成记录开始=========
............
............
............
2023-12-08 00:20:24.719 [pool-49-thread-1] INFO............
2023-12-08 00:20:24.719 [pool-49-thread-3] INFO............
2023-12-08 00:20:24.719 [pool-49-thread-4] INFO............
2023-12-08 00:20:24.719 [pool-49-thread-5] INFO............
............
............
............
2023-12-08 00:23:11.362 [Thread-95] INFO c.q.i.q.jobhandler.dataMongoDBXxlJob - ==========定时生成记录结束=========
分析原因:
集合是按月进行生成,集合名称"data-2023-12",新集合未自动添加相关索引
慢查询
增加索引
db.getCollection("data-2023-12").createIndex({
dataId: NumberInt("1"),
dateTime: NumberInt("1"),
pos: NumberInt("1")
}, {
name: "index_dataId_pos"
});
添加索引后定时任务执行时间,并且未触发cpu报警,耗时3秒左右
bash
2023-12-08 15:51:41.584 [Thread-97] INFO c.q.i.q.jobhandler.dataMongoDBXxlJob - ==========定时生成记录开始=========
..................
2023-12-08 15:51:41.810 [pool-51-thread-6] INFO c.q.i.query.service.ChargingStatisticsInfoService -
2023-12-08 15:51:41.810 [pool-51-thread-6] INFO c.q.i.query.service.ChargingStatisticsInfoService -
2023-12-08 15:51:41.858 [pool-51-thread-2] INFO c.q.i.query.service.ChargingStatisticsInfoService -
..................
2023-12-08 15:51:44.161 [Thread-97] INFO c.q.i.q.jobhandler.dataMongoDBXxlJob - ==========定时生成记录结束=========