MongoDB 3.2에서 "커버 된 쿼리"테스트를하고 있는데, 동일한 필드에서만 $or
표현을 포함하는 쿼리를 처리하지 못한다는 사실을 발견했습니다. 그러나 동일한 쿼리에서 $in
표현식을 $or
표현식으로 대체하면 정상적으로 작동합니다.
내가 테스트에 사용 된 인덱스는 다음이었다
db.test_collection.createIndex({ a: 1, b: 1, c: 1 });
이것은 인덱스
db.test_collection.find({
a: "string",
$or: [
{ b: true },
{ b: false },
],
c: "string"
}, { a: 1, b: 1, c: 1, _id: 0 });
적용 아니라는 $or
쿼리이며, 이것은 $in
쿼리입니다 그 은입니다.
db.test_collection.find({
a: "string",
b: { $in: [ true, false ] },
c: "string"
}, { a: 1, b: 1, c: 1, _id: 0 });
필드가 동일하고 투영법이 동일합니다. 그렇다면 색인에서 $or
검색어를 검색 할 수없는 이유는 무엇입니까?
이
는$or
쿼리에 대한
explain()
출력 :
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.test_collection",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"$or" : [
{
"b" : {
"$eq" : true
}
},
{
"b" : {
"$eq" : false
}
}
]
},
{
"a" : {
"$eq" : "string"
}
},
{
"c" : {
"$eq" : "string"
}
}
]
},
"winningPlan" : {
"stage" : "PROJECTION",
"transformBy" : {
"a" : 1,
"b" : 1,
"c" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"$or" : [
{
"b" : {
"$eq" : true
}
},
{
"b" : {
"$eq" : false
}
}
]
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"a" : 1,
"b" : 1,
"c" : 1
},
"indexName" : "a_1_b_1_c_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"a" : [
"[\"string\", \"string\"]"
],
"b" : [
"[MinKey, MaxKey]"
],
"c" : [
"[\"string\", \"string\"]"
]
}
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 2,
"executionTimeMillis" : 9,
"totalKeysExamined" : 2,
"totalDocsExamined" : 2,
"executionStages" : {
"stage" : "PROJECTION",
"nReturned" : 2,
"executionTimeMillisEstimate" : 10,
"works" : 3,
"advanced" : 2,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"transformBy" : {
"a" : 1,
"b" : 1,
"c" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "FETCH",
"filter" : {
"$or" : [
{
"b" : {
"$eq" : true
}
},
{
"b" : {
"$eq" : false
}
}
]
},
"nReturned" : 2,
"executionTimeMillisEstimate" : 10,
"works" : 3,
"advanced" : 2,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"docsExamined" : 2,
"alreadyHasObj" : 0,
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 2,
"executionTimeMillisEstimate" : 10,
"works" : 3,
"advanced" : 2,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"a" : 1,
"b" : 1,
"c" : 1
},
"indexName" : "a_1_b_1_c_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"a" : [
"[\"string\", \"string\"]"
],
"b" : [
"[MinKey, MaxKey]"
],
"c" : [
"[\"string\", \"string\"]"
]
},
"keysExamined" : 2,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
}
},
"serverInfo" : {
"host" : "VM-TOMLIN-HP",
"port" : 27017,
"version" : "3.2.6",
"gitVersion" : "05552b562c7a0b3143a729aaa0838e558dc49b25"
},
"ok" : 1
}
이이 $in
쿼리에 대한 explain()
출력 : 방식 때문에
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "test.test_collection",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"a" : {
"$eq" : "string"
}
},
{
"c" : {
"$eq" : "string"
}
},
{
"b" : {
"$in" : [
false,
true
]
}
}
]
},
"winningPlan" : {
"stage" : "PROJECTION",
"transformBy" : {
"a" : 1,
"b" : 1,
"c" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"a" : 1,
"b" : 1,
"c" : 1
},
"indexName" : "a_1_b_1_c_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"a" : [
"[\"string\", \"string\"]"
],
"b" : [
"[false, false]",
"[true, true]"
],
"c" : [
"[\"string\", \"string\"]"
]
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 2,
"executionTimeMillis" : 0,
"totalKeysExamined" : 2,
"totalDocsExamined" : 0,
"executionStages" : {
"stage" : "PROJECTION",
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 3,
"advanced" : 2,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"transformBy" : {
"a" : 1,
"b" : 1,
"c" : 1,
"_id" : 0
},
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 2,
"executionTimeMillisEstimate" : 0,
"works" : 3,
"advanced" : 2,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"a" : 1,
"b" : 1,
"c" : 1
},
"indexName" : "a_1_b_1_c_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"a" : [
"[\"string\", \"string\"]"
],
"b" : [
"[false, false]",
"[true, true]"
],
"c" : [
"[\"string\", \"string\"]"
]
},
"keysExamined" : 2,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
"serverInfo" : {
"host" : "VM-TOMLIN-HP",
"port" : 27017,
"version" : "3.2.6",
"gitVersion" : "05552b562c7a0b3143a729aaa0838e558dc49b25"
},
"ok" : 1
}
어떤 MongoDB 버전을 사용하고 있습니까? 또한 두 쿼리의'explain()'결과를 게시 할 수 있습니까? –
@KevinAdistambha 3.2.6을 사용 중입니다. 질문에'explain()'결과뿐만 아니라 이것을 추가했습니다. – Tom