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。为什么? –