MongoDB在负载下执行慢速读取查询

MongoDB在负载下执行慢速读取查询,mongodb,pymongo,Mongodb,Pymongo,我在VPS上运行MongoDB。同一个python机器人的两个实例不断地读写文档。本质上,他们不断地无限重复以下步骤: 使用find\u和

我在VPS上运行MongoDB。同一个python机器人的两个实例不断地读写文档。本质上,他们不断地无限重复以下步骤:

  • 使用
    find\u和
    获取文档(不,我不能在此处使用
    find()
  • 在步骤1中获取的过程数据(需要10-60秒)
  • 使用
    find\u and\u modify()
    更新文档(不,我不能在此处使用
    update()
  • 在我部署REST API web服务器以在浏览器等中查看结果之前,这一切都是完美无瑕的。现在,每当bot实例和web服务器同时运行时,bot的速度会降低几分钟,向我的web服务器发出请求需要2分钟。当机器人程序或web服务器正在运行时,它们都会在毫秒内查询数据库

    在我考虑升级我的VPS的RAM和CPU之前,我想了解到底是什么导致了这种情况。我假设问题在于数据库被来自3个客户端(2个bot实例,1个web服务器)的大量请求所淹没

    我找到了命令
    currentOp()
    ,但在理解其返回时遇到了问题

    > db.currentOp({"secs_running": {"$gte": 5}})
    {
            "inprog" : [
                    {
                            "type" : "op",
                            "host" : "localhost:27017",
                            "desc" : "conn1555",
                            "connectionId" : 1555,
                            "client" : "127.0.0.1:37750",
                            "clientMetadata" : {
                                    "driver" : {
                                            "name" : "PyMongo",
                                            "version" : "3.11.3"
                                    },
                                    "os" : {
                                            "type" : "Linux",
                                            "name" : "Linux",
                                            "architecture" : "x86_64",
                                            "version" : "5.4.0-71-generic"
                                    },
                                    "platform" : "CPython 3.8.5.final.0"
                            },
                            "active" : true,
                            "currentOpTime" : "2021-04-27T13:37:56.261+00:00",
                            "opid" : 3151480,
                            "lsid" : {
                                    "id" : UUID("71d36512-c5a3-4fe9-b3f9-31fd23e86413"),
                                    "uid" : BinData(0,"47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU=")
                            },
                            "secs_running" : NumberLong(123),
                            "microsecs_running" : NumberLong(123430156),
                            "op" : "command",
                            "ns" : "nxmain.websites",
                            "command" : {
                                    "aggregate" : "websites",
                                    "pipeline" : [
                                            {
                                                    "$match" : {
    
                                                    }
                                            },
                                            {
                                                    "$group" : {
                                                            "_id" : 1,
                                                            "n" : {
                                                                    "$sum" : 1
                                                            }
                                                    }
                                            }
                                    ],
                                    "cursor" : {
    
                                    },
                                    "lsid" : {
                                            "id" : UUID("71d36512-c5a3-4fe9-b3f9-31fd23e86413")
                                    },
                                    "$db" : "nxmain",
                                    "$readPreference" : {
                                            "mode" : "primaryPreferred"
                                    }
                            },
                            "planSummary" : "COLLSCAN",
                            "numYields" : 934,
                            "locks" : {
                                    "ReplicationStateTransition" : "w",
                                    "Global" : "r",
                                    "Database" : "r",
                                    "Collection" : "r"
                            },
                            "waitingForLock" : false,
                            "lockStats" : {
                                    "ReplicationStateTransition" : {
                                            "acquireCount" : {
                                                    "w" : NumberLong(936)
                                            }
                                    },
                                    "Global" : {
                                            "acquireCount" : {
                                                    "r" : NumberLong(936)
                                            }
                                    },
                                    "Database" : {
                                            "acquireCount" : {
                                                    "r" : NumberLong(936)
                                            }
                                    },
                                    "Collection" : {
                                            "acquireCount" : {
                                                    "r" : NumberLong(936)
                                            }
                                    },
                                    "Mutex" : {
                                            "acquireCount" : {
                                                    "r" : NumberLong(2)
                                            }
                                    }
                            },
                            "waitingForFlowControl" : false,
                            "flowControlStats" : {
    
                            }
                    },
                    {
                            "type" : "op",
                            "host" : "localhost:27017",
                            "desc" : "conn1535",
                            "connectionId" : 1535,
                            "client" : "127.0.0.1:36886",
                            "clientMetadata" : {
                                    "driver" : {
                                            "name" : "PyMongo",
                                            "version" : "3.11.3"
                                    },
                                    "os" : {
                                            "type" : "Linux",
                                            "name" : "Linux",
                                            "architecture" : "x86_64",
                                            "version" : "5.4.0-71-generic"
                                    },
                                    "platform" : "CPython 3.8.5.final.0"
                            },
                            "active" : true,
                            "currentOpTime" : "2021-04-27T13:37:56.261+00:00",
                            "opid" : 3152992,
                            "secs_running" : NumberLong(7),
                            "microsecs_running" : NumberLong(7503765),
                            "op" : "command",
                            "ns" : "admin.$cmd",
                            "command" : {
                                    "ismaster" : 1,
                                    "topologyVersion" : {
                                            "processId" : ObjectId("60845741af792cc59a636f20"),
                                            "counter" : NumberLong(0)
                                    },
                                    "maxAwaitTimeMS" : 10000,
                                    "$db" : "admin",
                                    "$readPreference" : {
                                            "mode" : "primary"
                                    }
                            },
                            "numYields" : 0,
                            "waitingForLatch" : {
                                    "timestamp" : ISODate("2021-04-27T13:37:48.858Z"),
                                    "captureName" : "AnonymousLatch"
                            },
                            "locks" : {
    
                            },
                            "waitingForLock" : false,
                            "lockStats" : {
    
                            },
                            "waitingForFlowControl" : false,
                            "flowControlStats" : {
    
                            }
                    }
            ],
            "ok" : 1
    }
    
    如您所见,管道中有两个操作。运行
    secs_:123
    的一个是web服务器,另一个是bot实例之一,当web服务器并行运行时,这大约需要10-20秒


    我的问题的原因是什么?我该如何解决?提前感谢。

    其中一个操作是管理命令。是web服务器还是机器人?聚合管道的用途是什么?可以分析慢速查询(更新、读取、删除)以找出原因-生成查询计划。@Sergio:admin命令是REST API的web服务器的命令@乔:说得好!再调查一下。聚合管道是mongodb在使用pymongo命令
    collection.count_documents({})
    对我的集合中的所有文档进行计数时所执行的操作。由于某种原因,当筛选器为永远占用的
    {}
    时(指定
    {“\u id”:{“$exists”:true}
    不会加快速度,但使用不推荐使用的命令
    collection.count()
    会加快速度)。但还是不太快。