首页 > 解决方案 > 如何在应用程序中调整 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;

标签: mongodb

解决方案


推荐阅读