tra*_*ein 2 indexing performance mongodb
我有一个包含大约100M文档的MongoDB集合.
文件基本上如下:
_id : ObjectId("asd1234567890")
_reference_1_id : ObjectId("fgh4567890123")
_reference_2_id : ObjectId("jkl7890123456")
name : "Test1"
id : "4815162342"
created_time : Date( 1331882436000 )
_contexts : ["context1", "context2"]
...
Run Code Online (Sandbox Code Playgroud)
设置了一些索引,这里是db.mycoll.getIndexes()的输出;
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"ns" : "mydb.mycoll",
"name" : "_id_"
},
{
"v" : 1,
"key" : {
"_reference_1_id" : 1,
"_reference_2_id" : 1,
"id" : 1
},
"unique" : true,
"ns" : "mydb.mycoll",
"name" : "_reference_1_id_1__reference_2_id_1_id_1"
},
{
"v" : 1,
"key" : {
"_reference_1_id" : 1,
"_reference_2_id" : 1,
"_contexts" : 1,
"created_time" : 1
},
"ns" : "mydb.mycoll",
"name" : "_reference_1_id_1__reference_2_id_1__contexts_1_created_time_1"
}
]
Run Code Online (Sandbox Code Playgroud)
当我执行像这样的查询时
db.mycoll.find({"_reference_2_id" : ObjectId("jkl7890123456")})
Run Code Online (Sandbox Code Playgroud)
无论是否有结果,它都需要一个多小时(!)才能完成.有任何想法吗?
更新: 这是输出的内容
db.mycoll.find({"_reference_2_id" : ObjectId("jkl7890123456")}).explain();
Run Code Online (Sandbox Code Playgroud)
好像:
{
"cursor" : "BasicCursor",
"nscanned" : 99209163,
"nscannedObjects" : 99209163,
"n" : 5007,
"millis" : 5705175,
"nYields" : 17389,
"nChunkSkips" : 0,
"isMultiKey" : false,
"indexOnly" : false,
"indexBounds" : {
}
}
Run Code Online (Sandbox Code Playgroud)
AD7*_*six 10
你没有任何mongo会自动使用的索引,所以它正在进行全表扫描.
如果查询中不存在[索引]的第一个键,则仅在显式提示时才使用索引.
为什么
如果你有a,b的索引 - 并且你a单独搜索- 将自动使用索引.这是因为它是索引的开始(这很快),db可以忽略索引值的其余部分.
单独搜索时,a,b上的索引效率很低,b因为它没有提供使用"以thisfixedstring开头"的索引搜索的可能性.
那么,要么:
暗示
可能是您现在最低成本的选择.
添加查询提示以强制使用_reference_1_id_1__reference_2_id_1_id_1索引.这可能比全表扫描快得多,但仍然比从您在查询中使用的字段开始的索引慢很多.
即
db.mycoll
.find({"_reference_2_id" : ObjectId("jkl7890123456")})
.hint("_reference_1_id_1__reference_2_id_1_id_1");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8562 次 |
| 最近记录: |