node.js - mongod进程的CPU总是99%,这正常吗?

标签 node.js mongodb

每当我在Ubuntu上运行nodeJS应用程序时,“ top”命令中的mongodb进程都会显示99%。这是mongodb本身的日志。

enter image description here

2018-12-19T06:27:08.085+0000 I COMMAND  [conn4022] command mydb.City command: find { find: "City", filter: { country: { $regex: "^United States$", $options: "i" }, state: { $regex: "^New York$", $options: "i" }, city: { $regex: "^New York$", $options: "i" } }, returnKey: false, showRecordId: false, lsid: { id: UUID("19d70135-fb6f-4729-8e8a-46958c127479") }, $db: "mydb" } planSummary: IXSCAN { country: 1, state: 1, city: 1 } keysExamined:82420 docsExamined:1 cursorExhausted:1 numYields:643 nreturned:1 reslen:301 locks:{ Global: { acquireCount: { r: 644 } }, Database: { acquireCount: { r: 644 } }, Collection: { acquireCount: { r: 644 } } } protocol:op_query 106ms
2018-12-19T06:27:08.190+0000 I COMMAND  [conn4021] command mydb.City command: find { find: "City", filter: { country: { $regex: "^United States$", $options: "i" }, state: { $regex: "^New York$", $options: "i" }, city: { $regex: "^New York$", $options: "i" } }, returnKey: false, showRecordId: false, lsid: { id: UUID("19d70135-fb6f-4729-8e8a-46958c127479") }, $db: "mydb" } planSummary: IXSCAN { country: 1, state: 1, city: 1 } keysExamined:82420 docsExamined:1 cursorExhausted:1 numYields:643 nreturned:1 reslen:301 locks:{ Global: { acquireCount: { r: 644 } }, Database: { acquireCount: { r: 644 } }, Collection: { acquireCount: { r: 644 } } } protocol:op_query 104ms
2018-12-19T06:27:08.297+0000 I COMMAND  [conn4022] command mydb.City command: find { find: "City", filter: { country: { $regex: "^United States$", $options: "i" }, state: { $regex: "^New York$", $options: "i" }, city: { $regex: "^New York$", $options: "i" } }, returnKey: false, showRecordId: false, lsid: { id: UUID("19d70135-fb6f-4729-8e8a-46958c127479") }, $db: "mydb" } planSummary: IXSCAN { country: 1, state: 1, city: 1 } keysExamined:82420 docsExamined:1 cursorExhausted:1 numYields:643 nreturned:1 reslen:301 locks:{ Global: { acquireCount: { r: 644 } }, Database: { acquireCount: { r: 644 } }, Collection: { acquireCount: { r: 644 } } } protocol:op_query 106ms
2018-12-19T06:27:08.402+0000 I COMMAND  [conn4021] command mydb.City command: find { find: "City", filter: { country: { $regex: "^United States$", $options: "i" }, state: { $regex: "^New York$", $options: "i" }, city: { $regex: "^New York$", $options: "i" } }, returnKey: false, showRecordId: false, lsid: { id: UUID("19d70135-fb6f-4729-8e8a-46958c127479") }, $db: "mydb" } planSummary: IXSCAN { country: 1, state: 1, city: 1 } keysExamined:82420 docsExamined:1 cursorExhausted:1 numYields:643 nreturned:1 reslen:301 locks:{ Global: { acquireCount: { r: 644 } }, Database: { acquireCount: { r: 644 } }, Collection: { acquireCount: { r: 644 } } } protocol:op_query 104ms
2018-12-19T06:27:08.506+0000 I COMMAND  [conn4022] command mydb.City command: find { find: "City", filter: { country: { $regex: "^United States$", $options: "i" }, state: { $regex: "^New York$", $options: "i" }, city: { $regex: "^Rochester$", $options: "i" } }, returnKey: false, showRecordId: false, lsid: { id: UUID("19d70135-fb6f-4729-8e8a-46958c127479") }, $db: "mydb" } planSummary: IXSCAN { country: 1, state: 1, city: 1 } keysExamined:82420 docsExamined:1 cursorExhausted:1 numYields:643 nreturned:1 reslen:302 locks:{ Global: { acquireCount: { r: 644 } }, Database: { acquireCount: { r: 644 } }, Collection: { acquireCount: { r: 644 } } } protocol:op_query 103ms


我的NodeJS应用程序只是在mongodb中搜索国家,城市和州的记录。

MongoClient.connect(url, {
    useNewUrlParser: true,
    reconnectTries: 60,
    reconnectInterval: 1000
}, (err, client) => {
    if (err) {
        console.error(`Connection Error: ${err}`)
        return process.exit(1);
    }

    clientc = client;
    client.topology.on('close', function(event) {
        console.log(JSON.stringify(event, null, 2));
        return process.exit(1);
    });


    client.topology.on('reconnect', function(event) {
        console.log(JSON.stringify(event, null, 2));
    });

    console.log(new Date().toString() + '>Connection successful.');

    db = client.db('mydb');
    collectionCity = db.collection('City');
    collectionCountry = db.collection('Country');
})

const findRecords = (collection, myobj) => {
    return new Promise((resolve, reject) => {
        collection.find(myobj).toArray((err, data) => {
            if (err) {
                console.error(`Cannot find records: ${err}`)
                resolve('error');
            } else {
                console.log(`Record found: ${data.length}`)
                resolve(data)
            }
        })
    });
}


这是在循环功能中搜索国家,州和代码的代码:

let cityquery = {
    country: {
        '$regex': country_s,
        $options: 'i'
    },
    state: {
        '$regex': state_s,
        $options: 'i'
    },
    city: {
        '$regex': city_s,
        $options: 'i'
    }
};
let cityrecord = await findRecords(collectionCity, cityquery);


市集索引:

> db.City.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "mydb.City"
        },
        {
                "v" : 2,
                "unique" : true,
                "key" : {
                        "country" : 1,
                        "state" : 1,
                        "city" : 1
                },
                "name" : "country_1_state_1_city_1",
                "ns" : "mydb.City"
        }
]


我不知道什么使CPU保持99%。我正在使用在AWS的Ubuntu上运行的mongodb v4.0.3。

最佳答案

如我所见,您使用不区分大小写的正则表达式搜索(“ i”选项)。

从MongoDB文档中:


  不区分大小写的正则表达式查询通常不能使用
  有效索引。 $ regex实现不支持排序规则
  并且无法利用不区分大小写的索引。


https://docs.mongodb.com/manual/reference/operator/query/regex/#index-use

因此,似乎根本就不使用您的索引,而是执行了完整扫描收集。

建议1:如果城市名称属于您数据库中的大写字母,则只需删除此“ I”选项

建议2:可能尝试使用'$ text'搜索https://docs.mongodb.com/manual/reference/operator/query/text/

关于node.js - mongod进程的CPU总是99%,这正常吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53845819/

相关文章:

javascript - 使用 node.js 的 HTTP 授权

javascript - NodeJS 中奇怪的 express.use() 行为

python - 如何检索两个字段值与 pymongo 相等的所有文档?

mongodb - Meteorjs Mongodb 如何使用 momentjs 查询日期

javascript - 已定义变量未定义

mongodb - 如何使用Golang比较两个bson.M数据集

javascript - AWS Lambda 函数多次处理相同的 dynamodb 流。我错过了什么?

node.js - 从套接字 io 访问 Express session

javascript - Meteor.js 和 Collection2 : Update Method that Inserts New Object in subschema

javascript - 了解嵌套回调和作用域?