首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >单进程CPU总是99%,这是正常的吗?

单进程CPU总是99%,这是正常的吗?
EN

Stack Overflow用户
提问于 2018-12-19 06:38:31
回答 1查看 333关注 0票数 1

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

代码语言:javascript
运行
复制
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搜索国家、城市和州的记录。

代码语言:javascript
运行
复制
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)
            }
        })
    });
}

这是循环函数中搜索国家、州和代码的代码:

代码语言:javascript
运行
复制
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);

城市收藏索引:

代码语言:javascript
运行
复制
> 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上使用MongoDBv4.0.3在Ubuntu上运行。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-20 14:45:51

正如我所看到的,您使用了不区分大小写的regexp搜索('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/

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53845819

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档