0
我從mongodb的文檔聽說MongoDB的索引和非索引性能
對於區分大小寫的正則表達式的查詢,一個索引是否存在該字段,那麼MongoDB的針對這些值的正則表達式匹配的索引,這可能比收集掃描更快。如果正則表達式是「前綴表達式」,則可能會發生進一步優化,這意味着所有可能的匹配都以相同的字符串開頭。這允許MongoDB根據該前綴構造一個「範圍」,並僅與來自該範圍內索引的那些值匹配。
查詢:
db.getCollection('contacts').find({username: {$regex: 'an'}}).explain()
這裏是沒有索引的統計username
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 14234,
"nscannedObjects" : 107721,
"nscanned" : 107721,
"nscannedObjectsAllPlans" : 107721,
"nscannedAllPlans" : 107721,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 841,
"nChunkSkips" : 0,
"millis" : 108,
"server" : "random-ubunto:3001",
"filterSet" : false
和統計信息與索引username
"cursor" : "BtreeCursor username_1",
"isMultiKey" : false,
"n" : 14234,
"nscannedObjects" : 14234,
"nscanned" : 106898,
"nscannedObjectsAllPlans" : 14234,
"nscannedAllPlans" : 106898,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 835,
"nChunkSkips" : 0,
"millis" : 142,
"indexBounds" : {
"username" : [
[
"",
{}
],
[
/an/,
/an/
]
]
},
"server" : "random-ubunto:3001",
"filterSet" : false
是的,我能看到的的區別。這很好,但問題是爲什麼索引編號的millis
大於沒有索引編號的編號。如果我們談論性能,millis
應該反之亦然。目前
millis (Without Indexing) : 108
millis (With Indexing) : 142
是的,我已經嘗試過。但沒有太大的區別。它的「毫米」大約在120-130之間。而沒有指數100-110。爲什麼? –