mongodb - 如何在应用程序中调整 Mongo 聚合操作?
问题描述
我们在生产中使用以下工作 Mongo 聚合查询,每天我们的调度程序每小时运行一次聚合查询。经验证,命名空间/集合“EventStore”上的聚合操作导致主 MongoDB 实例服务器上的高速缓存读取。这些聚合操作正在扫描 6344k 索引键和 6344k 文档。请让我知道这些操作是否可以在应用程序中进行调整。
IMongoCollection<BsonDocument> collection = mongoDbRepo.GetCollection<BsonDocument>("EventStore");
var options = new AggregateOptions()
{
AllowDiskUse = false
};
PipelineDefinition<BsonDocument, BsonDocument> pipeline = new BsonDocument[]
{
new BsonDocument("$match", new BsonDocument()
.Add("eventTs", new BsonDocument()
.Add("$gte", new BsonDateTime(startTs))
.Add("$lte", new BsonDateTime(endTs))
)
.Add("$or", new BsonArray()
.Add(new BsonDocument()
.Add("ev", "X1Added")
)
.Add(new BsonDocument()
.Add("ev", "X1Processed")
)
.Add(new BsonDocument()
.Add("ev", "X1Modified")
)
.Add(new BsonDocument()
.Add("ev", "X1Adjusted")
)
.Add(new BsonDocument()
.Add("ev", "X1Voided")
)
.Add(new BsonDocument()
.Add("ev", "X1Approved")
)
.Add(new BsonDocument()
.Add("ev", "X1Canceled")
)
.Add(new BsonDocument()
.Add("ev", "ZAdded")
)
.Add(new BsonDocument()
.Add("ev", "YAdded")
)
.Add(new BsonDocument()
.Add("ev", "YCanceled")
)
.Add(new BsonDocument()
.Add("ev", "YModified")
)
.Add(new BsonDocument()
.Add("ev", "YApproved")
)
.Add(new BsonDocument()
.Add("ev", "YChildAdded")
)
.Add(new BsonDocument()
.Add("ev", "YUpdated")
)
)),
new BsonDocument("$sort", new BsonDocument()
.Add("eid", 1.0)
.Add("eseq", 1.0)),
new BsonDocument("$group", new BsonDocument()
.Add("_id", "$eid")
.Add("lastSeq", new BsonDocument()
.Add("$last", "$eseq")
)
.Add("eventNames", new BsonDocument()
.Add("$push", "$ev")
)),
new BsonDocument("$lookup", new BsonDocument()
.Add("from", "Replication")
.Add("localField", "_id")
.Add("foreignField", "_eid")
.Add("as", "Replicated")),
new BsonDocument("$unwind", new BsonDocument()
.Add("path", "$Replicated")
.Add("preserveNullAndEmptyArrays", new BsonBoolean(true))),
new BsonDocument("$match", new BsonDocument()
.Add("$expr", new BsonDocument()
.Add("$or", new BsonArray()
.Add(new BsonDocument()
.Add("$ne", new BsonArray()
.Add("$Replicated.lastReplicationStatus")
.Add("success")
)
)
.Add(new BsonDocument()
.Add("$ne", new BsonArray()
.Add("$lastSeq")
.Add("$Replicated.lastReplicationSequence")
)
)
)
))
};
var cursor = await collection.AggregateAsync(pipeline, options);
List<BsonDocument> list = cursor.ToList();
return list;
解决方案
推荐阅读
- android - 红外 LED 与 Camera2 和 CameraX 的关系
- c++ - 如何将边界列表与数字列表一对一匹配
- java - 为什么我的循环在另一个语句之后执行,即使它位于 Android Studio 中的该语句之前
- wordpress - 列出具有自定义字段值的 WP 类别
- javascript - 如何有条件地对 JSON 数组进行排序和右移?
- cmake - cmake 是否可以选择避免使用未定义的变量(如 bash set-u)
- ios - 在 iOS 15 模拟器 (SwiftUI) 上未调用 onOpenURL
- python - 如何在远程机器上复制和执行文件?
- asp.net-core-2.0 - 如何在 IIS 中获取 Windows 登录用户名定义匿名 + Windows 身份验证
- javascript - React - 当用户点击提交时显示模式,在点击模式按钮之前不要点击端点