Mongodb 添加索引 优化记录

每晚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 - ==========定时生成记录结束=========
相关推荐
NGC_661111 分钟前
Java反射
数据库
huahailing102415 分钟前
Spring Boot 异步事务最佳实践:TransactionTemplate 实战指南
数据库·spring boot·后端
Data_Journal16 分钟前
如何将网站数据抓取到 Excel:一步步指南
大数据·开发语言·数据库·人工智能·php
米码收割机22 分钟前
【AI】OpenClaw问题排查
开发语言·数据库·c++·python
色空大师33 分钟前
【mysql建表避坑指南】
数据库·mysql
V1ncent Chen1 小时前
从零学SQL 02 MySQL架构介绍
数据库·sql·mysql·架构·数据分析
大母猴啃编程1 小时前
MySQL内置函数
数据库·sql·mysql·adb
@小匠1 小时前
Spring-Gateway-理论知识总结/常问面试题
数据库·spring·gateway
逍遥德1 小时前
postgresql数据库连接问题
数据库·postgresql
此方ls1 小时前
Redis源码研读八——listpack.c 1080-1528行
c语言·数据库·redis