简体   繁体   English

MongoDB 更改流非常慢

[英]MongoDB Change Streams very slow

I am encountering a delay of 5 to 10 seconds from when the operation happens in MongoDB until I capture it in a Change Stream in NodeJS.从 MongoDB 中的操作发生到我在 NodeJS 中的 Change Stream 中捕获它之前,我遇到了 5 到 10 秒的延迟。

Are these times normal, what parameters could I check to see if any are impacting this?这些时间是否正常,我可以检查哪些参数以查看是否有影响?

Here are a couple of examples and some suspicions (to be tested).这里有几个例子和一些怀疑(待测试)。

Here we try to catch changes only in the fields of the Users collection that interest us, I do not know if doing this to avoid unwanted events may be causing delay in the reception of the ChangeStream and it would be convenient to receive more events and filter in code the updated fields.这里我们尝试只捕获我们感兴趣的Users集合的字段中的更改,我不知道这样做是否会避免不需要的事件可能会导致ChangeStream的接收延迟并且接收更多事件和过滤器会很方便在代码中更新的字段。

I do not know, also if the "and" of the type of operation would have to be put before or it is irrelevant.我不知道,如果操作类型的“和”必须放在前面,或者它是不相关的,我也不知道。

userChangeStreamQuery: [{
    $match: {
        $and: [
            {$or:[
                { "updateDescription.updatedFields.name": { $exists: true } },
                { "updateDescription.updatedFields.email": { $exists: true } },
                { "updateDescription.updatedFields.organization": { $exists: true } },
                { "updateDescription.updatedFields.displayName": { $exists: true } },
                { "updateDescription.updatedFields.image": { $exists: true } },
                { "updateDescription.updatedFields.organizationName": { $exists: true } },
                { "updateDescription.updatedFields.locationName": { $exists: true } }
            ]},
            { operationType: "update" }]
    }
}],

Of this other one, that waits for events on the Plans collection, I worry that it does not have aggregate defined and it is when receiving the event where it is filtered if the operation arrives type 'insert', 'update', 'delete'.在另一个等待计划集合上的事件中,我担心它没有定义聚合,并且在接收到事件时,如果操作到达类型“插入”、“更新”、“删除”,它会被过滤. This one is giving us a delay of 7~10 seconds.这给了我们 7~10 秒的延迟。

startChangeStream({
    streamId: 'plans',
    collection: 'plans',
    query: '',
    resumeTokens
});
...
const startChangeStream = ({ streamId, collection, query, resumeTokens }) => {

    const resumeToken = resumeTokens ? resumeTokens[streamId] || undefined : undefined;

    nativeMongoDbFactory.setChangeStream({
        streamId,
        collection,
        query,
        resumeToken
    });
}

In no case are massive operations, normally they are operations performed by the user through web forms.在任何情况下都不是大规模操作,通常是用户通过 web forms 执行的操作。

when the collection is sharding, using change streams the mongos server need to wait until all shards have data to return, if some shards no data to write, the idle primary mongod writes a no-op to the oplog every 10 ( idlewriteperiodms ) seconds.当集合分片时,mongos服务器需要等待所有分片有数据返回,如果一些分片没有数据可写,空闲的主mongod每10( idlewriteperiodms )秒向oplog写入一个no-op。 that is why you delay is 7~10 seconds.这就是为什么你延迟是 7~10 秒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM