某一天,監控到mongo數據庫cpu使用率高了很多,查了一下,發現是下面這種語句引起的:
db.example_collection.find({
"idField" :
{ "$regex" : "123456789012345678"
} ,
"dateField" :
{ "$regex" : "2019/10/10"
}})
通常,遇到這種情況,我第一反應是缺少相關字段的索引,導致每執行一次這種語句都會全表掃描一次。
但是我用explain( )語句分析了下,發現上面所涉及的兩個字段idField、dateField是有索引的,并且該語句也是有使用到索引的。如下為explain( )的結果:
mgset-11111111:PRIMARY> db.example_collection.find({ "idField" : { "$regex" : "123456789012345678"} , "dateField" : { "$regex" : "2019/10/10"}}).explain("queryPlanner")
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "example_db.example_collection",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"idField" : {
"$regex" : "123456789012345678"
}
},
{
"dateField" : {
"$regex" : "2019/10/10"
}
}
]
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"filter" : {
"$and" : [
{
"idField" : {
"$regex" : "123456789012345678"
}
},
{
"dateField" : {
"$regex" : "2019/10/10"
}
}
]
},
"keyPattern" : {
"idField" : 1,
"dateField" : 1
},
"indexName" : "idField_1_dateField_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"idField" : [ ],
"dateField" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"idField" : [
"[\"\", {})",
"[/123456789012345678/, /123456789012345678/]"
],
"dateField" : [
"[\"\", {})",
"[/2019/10/10/, /2019/10/10/]"
]
}
}
},
"rejectedPlans" : [ ]
},
"ok" : 1
}
查看mongo的日志發現,這種語句執行一次就要800~900ms,的確是比較慢。除非數據庫cpu核數很多,要不然只要這種語句每秒并發稍微高一點,cpu很快就被占滿了。
之后搜索了下,發現有可能是正則表達式的問題。原來,雖然該語句的確是使用了索引,但是explain( )語句的輸出中還有一個字段"indexBounds",表示執行該語句時所需掃描的索引范圍。說實話,上面那個輸出中,我始終沒看明白它那個索引范圍。上面的語句對idField、dateField這兩個字段都進行了普通的正則表達式匹配,我猜測它應該是掃描了整個索引樹,所以導致索引并未實際提升該語句的查詢效率。
我看了下數據庫里面的數據,發現idField、dateField這兩個字段完全沒有必要進行正則匹配,進行普通的文本匹配就行。將正則匹配操作$regex去掉之后,再分析一下,結果是這樣的:
mgset-11111111:PRIMARY> db.example_collection.find({ "idField" : "123456789012345678", "dateField" : "2019/10/10"}).explain("queryPlanner")
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "example_db.example_collection",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"idField" : {
"$eq" : "123456789012345678"
}
},
{
"dateField" : {
"$eq" : "2019/10/10"
}
}
]
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"idField" : 1,
"dateField" : 1
},
"indexName" : "idField_1_dateField_1",
"isMultiKey" : false,
"multiKeyPaths" : {
"idField" : [ ],
"dateField" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"idField" : [
"[\"123456789012345678\", \"123456789012345678\"]"
],
"dateField" : [
"[\"2019/10/10\", \"2019/10/10\"]"
]
}
}
},
"rejectedPlans" : [ ]
},
"ok" : 1
}
可以看到,仍然使用到了索引,并且索引掃描范圍是僅限于一個值的。
后來跟開發人員確認了下,該語句確實沒必要使用正則匹配,就讓他把正則匹配去掉了。之后就沒有再出現問題了,mongo慢日志中也未再出現該語句。
總結
以上所述是小編給大家介紹的解決正則表示式匹配($regex)引起的一次mongo數據庫cpu占用率高的問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
您可能感興趣的文章:- 中文正則表達式匹配問題之正則表達式中文匹配使用方法
- Python正則表達式匹配日期與時間的方法
- Python正則表達式匹配數字和小數的方法
- python字符串中匹配數字的正則表達式
- Python正則表達式匹配和提取IP地址
- 一個正則表達式導致CPU 利用率居高不下