DBAtlas · 数据库运维图谱

查找未使用的索引并在删除前隐藏(hideIndex)

MongoDB性能

命令

// Usage counter per index (on this member, since the last restart):
db.musteri.aggregate([{ $indexStats: {} }, { $project: { name: 1, ops: "$accesses.ops", since: "$accesses.since" } }, { $sort: { ops: 1 } }])
// Hide it from the planner before dropping (the index keeps being maintained):
db.musteri.hideIndex("idx_email")
// Revert immediately if problems appear:
db.musteri.unhideIndex("idx_email")
// If the observation period passes without issues:
db.musteri.dropIndex("idx_email")

作用

$indexStats 显示每个索引被使用的次数;hideIndex 在不删除索引的情况下将其对查询规划器隐藏,从而可逆地测试移除它的影响。

适用场景

清理增加写入开销和存储占用的多余索引时。

注意

计数器按成员统计,重启后重置;若读取发往 secondary,请检查所有成员并观察足够长的时间(包括月末报表)。ops 为 0 的唯一索引或 TTL 索引可能仍然必要:唯一约束和 TTL 删除不会体现在计数器中。隐藏的索引在写入时仍会被维护,隐藏的唯一索引仍会强制约束。

在 DBAtlas 中打开 →

相关内容