简介
mongodb 的慢查询在操作系统中一般表现为 CPU 飙升;再应用中一般表现为返回缓慢;再优化过程中可以先针对参数进行优化,调整机器内存占用,然后开启慢查询记录,根据慢查询信息来选择优化方案。
参数调整
配置文件调整参数
可以将缓存大小设置为系统可用内存的 50%~70%。
# yaml格式
storage:
wiredTiger:
engineConfig:
cacheSizeGB: 4 # 设置 WiredTiger 缓存大小GB
# conf格式
wiredTigerCacheSizeGB=4 # 设置 WiredTiger 缓存大小GB
开启慢查询
检查是否开启
use admin
db.getProfilingStatus()
{ "was" : 0, "slowms" : 200 }
# 第一个值(0:表示没开启慢查询;1:表示记录慢查询;2:表示记录所有操作)
开启慢查询
修改配置文件
# yaml格式
operationProfiling:
mode: slowOp
slowOpThresholdMs: 1000
# conf格式
profile=1
slowms=1000
重启mongo即可
在线修改
mongo中需要按库修改慢查询
use database // 进入库
db.setProfilingLevel(1, 1000) // 开启慢查询并设置慢查询时间为 1000ms(1 秒)。
检查
use admin
db.getProfilingStatus()
{ "was" : 1, "slowms" : 1000 }
# 第一个值(0:表示没开启慢查询;1:表示记录慢查询;2:表示记录所有操作)
获取慢查询
先查询当前CPU占用信息,以确定优化前后的效果。
查询最慢的20条记录
use database
# 查询最慢的20条记录
db.system.profile.find().sort({ millis: -1 }).limit(10).pretty()
# 查询最近5条记录
db.system.profile.find().sort({ ts: -1 }).limit(5).pretty()
# 检查特定集合的慢查询
db.system.profile.find({ns: '你的数据库名.你的集合名'}).sort({ millis: -1 }).limit(10).pretty()
关键字段 | 含义与诊断价值 |
---|---|
planSummary |
显示查询的执行计划。如果值是 COLLSCAN (全集合扫描),通常就是性能瓶颈的明确信号,说明查询没有使用索引。 |
docsExamined |
数据库为了执行查询而扫描的文档数量。这个数值越大,说明查询效率越低。 |
keysExamined |
扫描的索引条目数量 。如果远大于 nreturned (返回的文档数),可能意味着索引效率不高或需要优化。 |
millis |
查询执行的总耗时(毫秒),是判断慢查询的直接依据。 |
查询当前操作
再navicat中会折叠一些关键信息,需要展开一些字段查看
db.currentOp()
inprog desc threadId connectionId client active opid secs_running microsecs_running op ns query query.f_vehicleGasLpn query.currentOp planSummary numYields locks waitingForLock lockStats appName
(Document) 16 Fields conn48 38524 48 19.196.5.254:52729 true 60140 1 1247886 update dss.realtime_gascc2025 (Document) 1 Field 吉ADN1633 COLLSCAN 2756 (Document) 3 Fields false (Document) 3 Fields
(Document) 16 Fields conn47 38184 47 19.196.5.254:52731 true 60157 0 674181 update dss.realtime_gascc2025 (Document) 1 Field 吉A17SH5 COLLSCAN 1764 (Document) 3 Fields false (Document) 3 Fields
(Document) 16 Fields conn46 35728 46 19.196.5.254:52728 true 59899 6 6070741 update dss.realtime_gascc2025 (Document) 1 Field 吉AK200挂 COLLSCAN 14162 (Document) 3 Fields false (Document) 3 Fields
(Document) 16 Fields conn45 38608 45 19.196.5.254:52726 true 60093 2 2834038 update dss.realtime_gascc2025 (Document) 1 Field 吉C1T567 COLLSCAN 6909 (Document) 3 Fields false
分析慢查询
explain 方法分析慢查询,获取详细的执行计划。
db.your_collection.find({ your_slow_query }).explain("executionStats")
创建索引
查看现有索引
use database
# 返回一个数组,详细列出集合中的所有索引
db.集合名字.getIndexes()
# 查看集合统计信息 返回集合的统计信息,其中包含索引总数和总索引大小等
db.集合名字.stats()
后台创建索引
在创建索引时必须选择后台创建索引,防止集合被锁定。
use database
db.realtime_gascc2025.createIndex({ "f_vehicleGasLpn": 1 }, { background: true })
# realtime_gascc2025:集合名字
# f_vehicleGasLpn:字段名
查询正在创建索引的操作
再navicat中会折叠一些关键信息,需要展开一些字段查看
db.currentOp({
$or: [
{ op: "command", "query.createIndexes": { $exists: true } },
{ op: "insert", ns: /\.system\.indexes\b/ },
{ op: "none", desc: /Index Build/ }
]
})
再 msg 信息中会显示创建索引的进度
inprog | desc | threadId | connectionId | client | appName | active | opid | secs_running | microsecs_running | op | ns | query | msg(重点) | progress | numYields | locks | waitingForLock | lockStats |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
(Document) 18 Fields | conn55 | 38568 | 55 | 127.0.0.1:52789 | Navicat | TRUE | 73365 | 73 | 73723096 | command | dss.$cmd | (Document) 2 Fields | Index Build (background) Index Build (background): 1060519/2345699 45% | (Document) 2 Fields | 8580 | (Document) 3 Fields | FALSE | (Document) 3 Fields |
查看CPU 是否下降。