속성이 a, b, c, d 및 e 인 문서의 경우mongodb 인덱스는 효율적으로 쿼리를 정확히 일치시켜야합니까?
a 및 b를 사용하여 쿼리를 실행하고 a, b 및 c를 포함하는 인덱스가있는 경우 두 번째 인덱스가 필요합니까? 그냥 a와 b를 다루거나, 현재 색인이 충분한가?
속성이 a, b, c, d 및 e 인 문서의 경우mongodb 인덱스는 효율적으로 쿼리를 정확히 일치시켜야합니까?
a 및 b를 사용하여 쿼리를 실행하고 a, b 및 c를 포함하는 인덱스가있는 경우 두 번째 인덱스가 필요합니까? 그냥 a와 b를 다루거나, 현재 색인이 충분한가?
정확한 색인을 반환하려면 a와 b의 현재 색인으로 충분합니다. 여기
는 예를s1:PRIMARY> db.users.insert({"username":"anil","email":"[email protected]","age":21})
WriteResult({ "nInserted" : 1 })
s1:PRIMARY> db.users.insert({"username":"sunil","email":"[email protected]","age":22})
WriteResult({ "nInserted" : 1 })
입니다 - 인덱스가
s1:PRIMARY> db.users.getIndexes();
[
{
"v" : 1,
"key" : {
"_id" : 1
},
"name" : "_id_",
"ns" : "projects.users"
},
{
"v" : 1,
"key" : {
"username" : 1,
"email" : 1,
"age" : 1
},
"name" : "username_1_email_1_age_1",
"ns" : "projects.users"
}
]
는 지금을 3 개 특성에 존재하는 경우 지금 3 개 특성에
s1:PRIMARY> db.users.createIndex({"username":1,"email":1,"age":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 1,
"numIndexesAfter" : 2,
"ok" : 1
}
확인 인덱스를 생성 executionStats 옵션을 사용하여 Explain 출력으로 찾으십시오.
s1:PRIMARY> db.users.find({"username":"sunil","email":"[email protected]"},{"username":1,"_id":0,"email":1}).explain("executionStats");
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "projects.users",
"indexFilterSet" : false,
"parsedQuery" : {
"$and" : [
{
"email" : {
"$eq" : "[email protected]"
}
},
{
"username" : {
"$eq" : "sunil"
}
}
]
},
"winningPlan" : {
"stage" : "PROJECTION",
"transformBy" : {
"username" : 1,
"_id" : 0,
"email" : 1
},
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"username" : 1,
"email" : 1,
"age" : 1
},
"indexName" : "username_1_email_1_age_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"username" : [
"[\"sunil\", \"sunil\"]"
],
"email" : [
"[\"[email protected]\", \"[email protected]\"]"
],
"age" : [
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans" : [ ]
},
"executionStats" : {
"executionSuccess" : true,
"nReturned" : 1,
"executionTimeMillis" : 0,
"totalKeysExamined" : 1,
"totalDocsExamined" : 0,
"executionStages" : {
"stage" : "PROJECTION",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 2,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"transformBy" : {
"username" : 1,
"_id" : 0,
"email" : 1
},
"inputStage" : {
"stage" : "IXSCAN",
"nReturned" : 1,
"executionTimeMillisEstimate" : 0,
"works" : 2,
"advanced" : 1,
"needTime" : 0,
"needYield" : 0,
"saveState" : 0,
"restoreState" : 0,
"isEOF" : 1,
"invalidates" : 0,
"keyPattern" : {
"username" : 1,
"email" : 1,
"age" : 1
},
"indexName" : "username_1_email_1_age_1",
"isMultiKey" : false,
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 1,
"direction" : "forward",
"indexBounds" : {
"username" : [
"[\"sunil\", \"sunil\"]"
],
"email" : [
"[\"[email protected]\", \"[email protected]\"]"
],
"age" : [
"[MinKey, MaxKey]"
]
},
"keysExamined" : 1,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0
}
}
},
"serverInfo" : {
"host" : "L4156409",
"port" : 47018,
"version" : "3.2.5",
"gitVersion" : "34e65e5383f7ea1726332cb175b73077ec4a1b02"
},
"ok" : 1
}
s1:PRIMARY>
totalKeysExamined = 1 및 totalDocsExamined = 0 및 noOfDocuments가 반환되었습니다 = 1 이는 검색된 문서가 색인 자체에서 나온 것임을 의미합니다.
이는 덮여 조회로 알려져 있습니다.
HTH.
아니요, 인덱스가 정렬 된 순서 (a, b, c)로 쿼리하기 때문에 추가 인덱스가 필요하지 않습니다. b, a로 검색해야하는 경우 새 색인을 사용하는 것이 좋습니다.