每当我在Ubuntu上运行nodeJS应用程序时,“ top”命令中的mongodb进程都会显示99%。这是mongodb本身的日志。
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。
答案 0 :(得分:0)
我看到您使用不区分大小写的正则表达式搜索(“ 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/