我使用facet来减少索引内存分配。
以下是其中一个文档(集合中超过一百万个文档)的示例:
{
"_id": ObjectId("5461c8f1426f727f169d0000"),
"n": "X-Men: Days of Future Past",
"f": [
{
"t": "ry",
"v": 2014
},
{
"t": "g",
"v": [
"Action",
"Adventure",
"Sci-Fi"
]
},
{
"t": "ml",
"v": "English"
},
{
"t": "k",
"v": "movie"
},
{
"t": "rel",
"v": true
}
],
ai: 1898267,
ec: 3
}
还有更多字段,但与案例无关。
" f"字段描述方面。这些是我为查询创建的索引:
{
"f.t" : 1,
"f.v" : 1,
"ai" : -1
}
和
{ ec: -1 }
现在,我试图执行的查询是:
db.items.find({"$and": [
{"f": {"$elemMatch": {t: "ml", v: {"$in": ["English", "Spanish", "French", "German"]}}}},
{"f": {"$elemMatch": {t: "rel", v: true}}},
{"f": {"$elemMatch": {t: "k", v: "movie"}}},
{"f": {"$elemMatch": {t: "ry", v: 2014}}},
{"f": {"$elemMatch": {t: "g", v: {"$in": ["Action"]}}}}
]
}).limit(12)
需要永远。我担心服务器级别的内存分配可能存在一些问题。
这是explain()文档:
{
"cursor": "BtreeCursor f.t_1_f.v_1_ai_-1",
"isMultiKey": true,
"n": 12,
"nscannedObjects": 228327,
"nscanned": 228327,
"nscannedObjectsAllPlans": 228327,
"nscannedAllPlans": 228327,
"scanAndOrder": false,
"indexOnly": false,
"nYields": 87096,
"nChunkSkips": 0,
"millis": 120605,
"indexBounds": {
"f.t": [
[
"rel",
"rel"
]
],
"f.v": [
[
true,
true
]
],
"ai": [
[{
"$maxElement": 1
}, {
"$minElement": 1
}]
]
},
"server": "h00222.mongolab.com:42100",
"filterSet": false,
"stats": {
"type": "LIMIT",
"works": 315226,
"yields": 87096,
"unyields": 87096,
"invalidates": 0,
"advanced": 12,
"needTime": 228315,
"needFetch": 86899,
"isEOF": 1,
"children": [{
"type": "KEEP_MUTATIONS",
"works": 315226,
"yields": 87096,
"unyields": 87096,
"invalidates": 0,
"advanced": 12,
"needTime": 228315,
"needFetch": 86899,
"isEOF": 0,
"children": [{
"type": "FETCH",
"works": 315226,
"yields": 87096,
"unyields": 87096,
"invalidates": 0,
"advanced": 12,
"needTime": 228315,
"needFetch": 86899,
"isEOF": 0,
"alreadyHasObj": 0,
"forcedFetches": 0,
"matchTested": 12,
"children": [{
"type": "IXSCAN",
"works": 228327,
"yields": 87096,
"unyields": 87096,
"invalidates": 0,
"advanced": 228327,
"needTime": 0,
"needFetch": 0,
"isEOF": 0,
"keyPattern": "{ f.t: 1.0, f.v: 1.0, ai: -1.0 }",
"isMultiKey": 1,
"boundsVerbose": "field #0['f.t']: [\"rel\", \"rel\"], field #1['f.v']: [true, true], field #2['ai']: [MaxKey, MinKey]",
"yieldMovedCursor": 0,
"dupsTested": 228327,
"dupsDropped": 0,
"seenInvalidated": 0,
"matchTested": 0,
"keysExamined": 228327,
"children": []
}]
}]
}]
}
}
关于可能发生的事情的任何想法?
谢谢。