Mongo selecting the wrong index - mongodb

I'm using mongo db 2.6.9.
I created 2 indexes on a collection and i don`t understand the reson that the query planer allways selects the wrong index.
I would like to understand what I'm missing.
First Index:
{
"TimeStamp":1,
"A":1,
"B":1,
}
Second Index:
{
"TimeStamp":1,
"A":1,
"C":1,
}
When I'm useing the following query the planner selects the first index:
db.collection.find({"TimeStamp":{ "$gte" : ISODate("2015-04-14T00:00:00Z"), "$lt" : ISODate("2015-04-15T00:00:00Z") },"C":2137,"A":1}).explain()
explain({verbose:1}) results:
{
"cursor" : "BtreeCursor IX_First",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 0,
"nscannedObjectsAllPlans" : 0,
"nscannedAllPlans" : 0,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 0,
"indexBounds" : {
"TimeStamp" : [
[
ISODate("2015-04-14T00:00:00Z"),
ISODate("2015-04-15T00:00:00Z")
]
],
"A" : [
[
1,
1
]
],
"B" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
},
"allPlans" : [
{
"cursor" : "BtreeCursor IX_First",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 0,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"TimeStamp" : [
[
ISODate("2015-04-14T00:00:00Z"),
ISODate("2015-04-15T00:00:00Z")
]
],
"A" : [
[
1,
1
]
],
"B" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
},
{
"cursor" : "BtreeCursor IIX_Second",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 0,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"TimeStamp" : [
[
ISODate("2015-04-14T00:00:00Z"),
ISODate("2015-04-15T00:00:00Z")
]
],
"A" : [
[
1,
1
]
],
"C" : [
[
2137,
2137
]
]
}
}
],
"server" : "mongo2:27017",
"filterSet" : false,
"stats" : {
"type" : "KEEP_MUTATIONS",
"works" : 2,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 0,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"children" : [
{
"type" : "FETCH",
"works" : 1,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 0,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 0,
"children" : [
{
"type" : "IXSCAN",
"works" : 1,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 0,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ TimeStamp: 1, A: 1, B: 1}",
"isMultiKey" : 0,
"boundsVerbose" : "field #0['TimeStamp']: [new Date(1428969600000), new Date(1429056000000)), field #1['A']: [1.0, 1.0], field #2['B']: [MinKey, MaxKey]",
"yieldMovedCursor" : 0,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 0,
"children" : [ ]
}
]
}
]
}
}

That's because the order of the index fields matters in this case. Try swapping A and C:
db.collection.find({"TimeStamp":{ "$gte" : ISODate("2015-04-14T00:00:00Z"), "$lt" : ISODate("2015-04-15T00:00:00Z") },"A":1,"C":2137}).explain()

Related

Mongodb log show they're using IXScan but db.currentOp() no

On mongodb log shows they're using IXScan
[conn37225] command hone.$cmd command: count { count: "events", query:
{ eventName: "Started", createdAt: { $gt: new Date(1449789260690),
$lte: new Date(1450221260693) }, data.quizId:
"54fe05f26aa23aeb72e3fdbb" }, fields: null } planSummary: IXSCAN {
createdAt: 1, data.quizId: 1, eventName: 1 } keyUpdates:0
numYields:4279 locks(micros) r:53800992 reslen:48 38679ms
but on db.currentOp() don't
{
"opid" : 4719819,
"active" : true,
"secs_running" : 4,
"microsecs_running" : NumberLong(4221829),
"op" : "query",
"ns" : "hone.events",
"query" : {
"count" : "events",
"query" : {
"eventName" : "Started",
"createdAt" : {
"$gt" : ISODate("2015-12-10T23:14:20.690Z"),
"$lte" : ISODate("2015-12-15T23:14:20.693Z")
},
"data.quizId" : "54fe05f26aa23aeb72e3fdbb"
},
"fields" : null
},
"client" : "10.8.0.7:43823",
"desc" : "conn37225",
"threadId" : "0x7f60aee9a700",
"connectionId" : 37225,
"locks" : {
"^" : "r",
"^hone" : "R"
},
"waitingForLock" : false,
"numYields" : 384,
"lockStats" : {
"timeLockedMicros" : {
"r" : NumberLong(6417513),
"w" : NumberLong(0)
},
"timeAcquiringMicros" : {
"r" : NumberLong(800491),
"w" : NumberLong(0)
}
}
}
Is the mongodb using properly the { createdAt: 1, data.quizId: 1, eventName: 1 } index?
using hint() with explain() is possible see the difference.
with hint() use specifically that index and works fast.
I'm wondering know what is causing it to be slower.
Without hint
> db.events.find({ eventName: "Completed", createdAt: { $gt: new Date(1449945185127), $lte: new Date(1450377185140) }, "data.quizId": "55ce6dde5239249a70f9ef54" }).explain("executionStats")
{
"cursor" : "BtreeCursor createdAt_1_data.quizId_1_eventName_1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 2519326,
"nscannedObjectsAllPlans" : 2543445,
"nscannedAllPlans" : 7536363,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 79008,
"nChunkSkips" : 0,
"millis" : 74443,
"indexBounds" : {
"createdAt" : [
[
ISODate("2015-12-12T18:33:05.127Z"),
ISODate("2015-12-17T18:33:05.140Z")
]
],
"data.quizId" : [
[
"55ce6dde5239249a70f9ef54",
"55ce6dde5239249a70f9ef54"
]
],
"eventName" : [
[
"Completed",
"Completed"
]
]
},
"allPlans" : [
{
"cursor" : "BtreeCursor createdAt_1_data.quizId_1_eventName_1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 2519326,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"createdAt" : [
[
ISODate("2015-12-12T18:33:05.127Z"),
ISODate("2015-12-17T18:33:05.140Z")
]
],
"data.quizId" : [
[
"55ce6dde5239249a70f9ef54",
"55ce6dde5239249a70f9ef54"
]
],
"eventName" : [
[
"Completed",
"Completed"
]
]
}
},
{
"cursor" : "BtreeCursor eventName_1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 2497710,
"nscanned" : 2497710,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"eventName" : [
[
"Completed",
"Completed"
]
]
}
},
{
"cursor" : "BtreeCursor createdAt_1_data.questionId_1_eventName_1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 45735,
"nscanned" : 2519327,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"createdAt" : [
[
ISODate("2015-12-12T18:33:05.127Z"),
ISODate("2015-12-17T18:33:05.140Z")
]
],
"data.questionId" : [
[
{
"$minElement" : 1
},
{
"$maxElement" : 1
}
]
],
"eventName" : [
[
"Completed",
"Completed"
]
]
}
}
],
"server" : "mongodb-05:27017",
"filterSet" : false,
"stats" : {
"type" : "FETCH",
"works" : 2519328,
"yields" : 79008,
"unyields" : 79008,
"invalidates" : 0,
"advanced" : 0,
"needTime" : 2519326,
"needFetch" : 0,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 0,
"children" : [
{
"type" : "IXSCAN",
"works" : 2519327,
"yields" : 79008,
"unyields" : 79008,
"invalidates" : 0,
"advanced" : 0,
"needTime" : 2519326,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ createdAt: 1, data.quizId: 1, eventName: 1 }",
"isMultiKey" : 0,
"boundsVerbose" : "field #0['createdAt']: (new Date(1449945185127), new Date(1450377185140)], field #1['data.quizId']: [\"55ce6dde5239249a70f9ef54\", \"55ce6dde5239249a70f9ef54\"], field #2['eventName']: [\"Completed\", \"Completed\"]",
"yieldMovedCursor" : 0,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 2519326,
"children" : [ ]
}
]
}
}
With hint
> db.events.find({ eventName: "Completed", createdAt: { $gt: new Date(1449945185127), $lte: new Date(1450377185140) }, "data.quizId": "55ce6dde5239249a70f9ef54" }).hint('createdAt_1_data.quizId_1_eventName_1').explain("executionStats")
{
"cursor" : "BtreeCursor createdAt_1_data.quizId_1_eventName_1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 2519326,
"nscannedObjectsAllPlans" : 0,
"nscannedAllPlans" : 2519326,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 19682,
"nChunkSkips" : 0,
"millis" : 8211,
"indexBounds" : {
"createdAt" : [
[
ISODate("2015-12-12T18:33:05.127Z"),
ISODate("2015-12-17T18:33:05.140Z")
]
],
"data.quizId" : [
[
"55ce6dde5239249a70f9ef54",
"55ce6dde5239249a70f9ef54"
]
],
"eventName" : [
[
"Completed",
"Completed"
]
]
},
"allPlans" : [
{
"cursor" : "BtreeCursor createdAt_1_data.quizId_1_eventName_1",
"isMultiKey" : false,
"n" : 0,
"nscannedObjects" : 0,
"nscanned" : 2519326,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"createdAt" : [
[
ISODate("2015-12-12T18:33:05.127Z"),
ISODate("2015-12-17T18:33:05.140Z")
]
],
"data.quizId" : [
[
"55ce6dde5239249a70f9ef54",
"55ce6dde5239249a70f9ef54"
]
],
"eventName" : [
[
"Completed",
"Completed"
]
]
}
}
],
"server" : "mongodb-05:27017",
"filterSet" : false,
"stats" : {
"type" : "FETCH",
"works" : 2519327,
"yields" : 19682,
"unyields" : 19682,
"invalidates" : 0,
"advanced" : 0,
"needTime" : 2519326,
"needFetch" : 0,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 0,
"children" : [
{
"type" : "IXSCAN",
"works" : 2519327,
"yields" : 19682,
"unyields" : 19682,
"invalidates" : 0,
"advanced" : 0,
"needTime" : 2519326,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ createdAt: 1, data.quizId: 1, eventName: 1 }",
"isMultiKey" : 0,
"boundsVerbose" : "field #0['createdAt']: (new Date(1449945185127), new Date(1450377185140)], field #1['data.quizId']: [\"55ce6dde5239249a70f9ef54\", \"55ce6dde5239249a70f9ef54\"], field #2['eventName']: [\"Completed\", \"Completed\"]",
"yieldMovedCursor" : 0,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 2519326,
"children" : [ ]
}
]
}
}

count slow in mongodb 2.6.8

I've a find request which takes 0.031sec, but when I try to do the same request with a count, it takes over 1sec.
I tried different indexes but it's always the same problem.
Count is still slow.
any idea?
volume
1600000 documents
My request
db.books.find(
{
"categories" : { $eq : null},
"theme" : "comics"
}
)
My Index
{
"categories" : 1,
"theme" : 1
}
Explain
{
"cursor" : "BtreeCursor categories_1_theme_1",
"isMultiKey" : false,
"n" : 353912,
"nscannedObjects" : 353912,
"nscanned" : 353912,
"nscannedObjectsAllPlans" : 354821,
"nscannedAllPlans" : 354821,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 2771,
"nChunkSkips" : 0,
"millis" : 1111,
"indexBounds" : {
"theme" : [
[
"comics",
"comics"
]
],
"categories" : [
[
null,
null
]
]
},
"server" : "xxxmongoxxx:27017",
"filterSet" : false,
"stats" : {
"type" : "KEEP_MUTATIONS",
"works" : 353913,
"yields" : 2771,
"unyields" : 2771,
"invalidates" : 0,
"advanced" : 353912,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"children" : [
{
"type" : "FETCH",
"works" : 353913,
"yields" : 2771,
"unyields" : 2771,
"invalidates" : 0,
"advanced" : 353912,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 353912,
"children" : [
{
"type" : "IXSCAN",
"works" : 353913,
"yields" : 2771,
"unyields" : 2771,
"invalidates" : 0,
"advanced" : 353912,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ categories: 1, theme: 1 }",
"isMultiKey" : 0,
"boundsVerbose" : "field #0['categories']: [\"comics\", \"comics\"], field #1['theme']: [null, null]",
"yieldMovedCursor" : 0,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 353912,
"children" : []
}
]
}
]
}
}

Why indexOnly==false

I have a collection with index:
{
"UserId" : 1,
"ShareId" : 1,
"ParentId" : 1,
"DeletedDate" : 1
}
If I making query:
db.Files.find({ "UserId" : ObjectId("5450d837f32a1e098c844e2a"),
"ShareId" : ObjectId("5450d879f32a1e098c844e94"),
"ParentId" : ObjectId("5450d8af6a092a0b74a44026"),
"DeletedDate":null},
{_id:0, ShareId:1}).explain()
output says that "indexOnly" : false:
{
"cursor" : "BtreeCursor UserId_1_ShareId_1_ParentId_1_DeletedDate_1",
"isMultiKey" : false,
"n" : 2120,
"nscannedObjects" : 2120,
"nscanned" : 2120,
"nscannedObjectsAllPlans" : 2318,
"nscannedAllPlans" : 2320,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 21,
"nChunkSkips" : 0,
"millis" : 42,
"indexBounds" : {
"UserId" : [
[
ObjectId("5450d837f32a1e098c844e2a"),
ObjectId("5450d837f32a1e098c844e2a")
]
],
"ShareId" : [
[
ObjectId("5450d879f32a1e098c844e94"),
ObjectId("5450d879f32a1e098c844e94")
]
],
"ParentId" : [
[
ObjectId("5450d8af6a092a0b74a44026"),
ObjectId("5450d8af6a092a0b74a44026")
]
],
"DeletedDate" : [
[
null,
null
]
]
},
"server" : "mongowecntprod:27017",
"filterSet" : false,
"stats" : {
"type" : "PROJECTION",
"works" : 2124,
"yields" : 21,
"unyields" : 21,
"invalidates" : 0,
"advanced" : 2120,
"needTime" : 0,
"needFetch" : 2,
"isEOF" : 1,
"children" : [
{
"type" : "KEEP_MUTATIONS",
"works" : 2124,
"yields" : 21,
"unyields" : 21,
"invalidates" : 0,
"advanced" : 2120,
"needTime" : 1,
"needFetch" : 2,
"isEOF" : 1,
"children" : [
{
"type" : "FETCH",
"works" : 2124,
"yields" : 21,
"unyields" : 21,
"invalidates" : 0,
"advanced" : 2120,
"needTime" : 1,
"needFetch" : 2,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 2120,
"children" : [
{
"type" : "IXSCAN",
"works" : 2121,
"yields" : 21,
"unyields" : 21,
"invalidates" : 0,
"advanced" : 2120,
"needTime" : 1,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ UserId: 1, ShareId: 1, ParentId: 1, DeletedDate: 1 }",
"isMultiKey" : 0,
"boundsVerbose" : "field #0['UserId']: [ObjectId('5450d837f32a1e098c844e2a'), ObjectId('5450d837f32a1e098c844e2a')], field #1['ShareId']: [ObjectId('5450d879f32a1e098c844e94'), ObjectId('5450d879f32a1e098c844e94')], field #2['ParentId']: [ObjectId('5450d8af6a092a0b74a44026'), ObjectId('5450d8af6a092a0b74a44026')], field #3['DeletedDate']: [null, null]",
"yieldMovedCursor" : 0,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 2120,
"children" : []
}
]
}
]
}
]
}
}
but if I making query without DeletedDate:
db.Files.find({ "UserId" : ObjectId("5450d837f32a1e098c844e2a"),
"ShareId" : ObjectId("5450d879f32a1e098c844e94"),
"ParentId" : ObjectId("5450d8af6a092a0b74a44026")},
{_id:0, ShareId:1}).explain()
then "indexOnly" is true.
How I can change first query to making indexOnly=true?
Let me give you a simple example that will hopefully demonstrate what you're seeing when you are querying for a field being null:
db.nullexplain.find()
{ "_id" : ObjectId("5456759f51a9d5271dc55bba"), "a" : 1 }
{ "_id" : ObjectId("545675a251a9d5271dc55bbb"), "a" : null }
{ "_id" : ObjectId("545675a551a9d5271dc55bbc") }
db.nullexplain.ensureIndex({a:1})
db,nullexplain.count({a:1}).count()
1
db.nullexplain.count({a:null}).count()
2
Do you see the issue? When "a" is present and explicitly set to null, it's indexed as null.
When "a" is not present in the document, it's also indexed as null.
When you query:
db.nullexplain.find({a:null},{_id:0,a:1})
{ "a" : null }
{ }
How can we derive from the index only whether the return document should have the field "a" set to null or if the field should not be present at all?
The answer is we cannot and therefore we must examine the document itself.
db.nullexplain.find({a:null},{_id:0,a:1}).explain()
{
"cursor" : "BasicCursor",
"isMultiKey" : false,
"n" : 2,
"nscannedObjects" : 3,
"nscanned" : 3,
"nscannedObjectsAllPlans" : 3,
"nscannedAllPlans" : 3,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 3,
"server" : "Asyas-MacBook-Pro.local:27017",
"filterSet" : false
}
Hope this helps you understand why querying for DeletedDate:null has to check the document and cannot be answered from the index.

MongoDB refuses to use index intersection

I use MongoDB 2.6.4.
My indexes looks like this:
{
"v" : 1,
"key" : {
"isFolder" : 1
},
"name" : "isFolder_1",
"ns" : "Tenant_51.files",
"background" : true
},
{
"v" : 1,
"key" : {
"isForeign" : 1
},
"name" : "isForeign_1",
"ns" : "Tenant_51.files",
"background" : true
},
My query looks like this:
db.files.find({ isFolder: true, isForeign: false }).explain(true)
For some reason, it chooses to use only 1 index (VERY SLOW: 680 seconds!!)
It looks like it does calculate the Complex Plan, however, decides not to use it, and I don't understand why.
Here is the execution plan:
{
"cursor" : "BtreeCursor isFolder_1",
"isMultiKey" : false,
"n" : 107441,
"nscannedObjects" : 110580,
"nscanned" : 110580,
"nscannedObjectsAllPlans" : 110689,
"nscannedAllPlans" : 110801,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 21056,
"nChunkSkips" : 0,
"millis" : 679121,
"indexBounds" : {
"isFolder" : [
[
true,
true
]
]
},
"allPlans" : [
{
"cursor" : "BtreeCursor isFolder_1",
"isMultiKey" : false,
"n" : 107441,
"nscannedObjects" : 110580,
"nscanned" : 110580,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"isFolder" : [
[
true,
true
]
]
}
},
{
"cursor" : "BtreeCursor isForeign_1",
"isMultiKey" : false,
"n" : 68,
"nscannedObjects" : 109,
"nscanned" : 110,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
"isForeign" : [
[
false,
false
]
]
}
},
{
"cursor" : "Complex Plan",
"n" : 42,
"nscannedObjects" : 0,
"nscanned" : 111,
"nChunkSkips" : 0
}
],
"server" : "XXX",
"filterSet" : false,
"stats" : {
"type" : "KEEP_MUTATIONS",
"works" : 128743,
"yields" : 21056,
"unyields" : 21056,
"invalidates" : 13834,
"advanced" : 107441,
"needTime" : 3140,
"needFetch" : 18161,
"isEOF" : 1,
"children" : [
{
"type" : "FETCH",
"works" : 128743,
"yields" : 21056,
"unyields" : 21056,
"invalidates" : 13834,
"advanced" : 107441,
"needTime" : 3140,
"needFetch" : 18161,
"isEOF" : 1,
"alreadyHasObj" : 0,
"forcedFetches" : 0,
"matchTested" : 107441,
"children" : [
{
"type" : "IXSCAN",
"works" : 110581,
"yields" : 21056,
"unyields" : 21056,
"invalidates" : 13834,
"advanced" : 110580,
"needTime" : 1,
"needFetch" : 0,
"isEOF" : 1,
"keyPattern" : "{ isFolder: 1 }",
"isMultiKey" : 0,
"boundsVerbose" : "field #0['isFolder']: [true, true]",
"yieldMovedCursor" : 0,
"dupsTested" : 0,
"dupsDropped" : 0,
"seenInvalidated" : 0,
"matchTested" : 0,
"keysExamined" : 110580,
"children" : [ ]
}
]
}
]
}
}
From the MongoDB docs about indexing:
MongoDB can only use one index to support any given operation.
The solution, however, is as easy as the explanation: Use a compound index.
db.files.ensureIndex({isFolder:1,isForeign:1})

Why so many scanned Objects?

I have a table with the following objects:
> db.shapes.find()
{ "_id" : "P1", "amenity" : "restaurant", "shape" : { "type" : "Point",
"coordinates" : [ 2, 2 ] } }
{ "_id" : "P2", "amenity" : "restaurant", "shape" : { "type" : "Point",
"coordinates" : [ 2, 4 ] } }
{ "_id" : "P3", "amenity" : "police", "shape" : { "type" : "Point",
"coordinates" : [ 4, 2 ] } }
{ "_id" : "P4", "amenity" : "police", "shape" : { "type" : "Point",
"coordinates" : [ 4, 4 ] } }
The explain() on the following query gives a strange (in my opinion) result:
> db.shapes.find({shape:{$nearSphere:{$geometry:{type: "Point", coordinates:
[0,0]}}}}, {id:1, amenity:1}).limit(2).explain()
{
"cursor" : "S2NearCursor",
"isMultiKey" : false,
"n" : 2,
"nscannedObjects" : 22,
"nscanned" : 22,
"nscannedObjectsAllPlans" : 22,
"nscannedAllPlans" : 22,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 1,
"indexBounds" : {
},
"server" : "DBTest:27017",
"filterSet" : false
}
Why there are this much objects scanned ? I mean, the table only has 4
objects and mongodb scans 22 ?
I am thankful for any explanation.
Bye, Andre
> db.shapes.find({shape:{$nearSphere:{$geometry:{type: "Point", coordinates:
... [0,0]}}}}, {id:1, amenity:1}).limit(2).explain(1)
{
"cursor" : "S2NearCursor",
"isMultiKey" : false,
"n" : 2,
"nscannedObjects" : 22,
"nscanned" : 22,
"nscannedObjectsAllPlans" : 22,
"nscannedAllPlans" : 22,
"scanAndOrder" : false,
"indexOnly" : false,
"nYields" : 0,
"nChunkSkips" : 0,
"millis" : 1,
"indexBounds" : {
},
"allPlans" : [
{
"cursor" : "S2NearCursor",
"isMultiKey" : false,
"n" : 2,
"nscannedObjects" : 22,
"nscanned" : 22,
"scanAndOrder" : false,
"indexOnly" : false,
"nChunkSkips" : 0,
"indexBounds" : {
}
}
],
"server" : "DBTest:27017",
"filterSet" : false,
"stats" : {
"type" : "LIMIT",
"works" : 22,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 2,
"needTime" : 20,
"needFetch" : 0,
"isEOF" : 1,
"children" : [
{
"type" : "PROJECTION",
"works" : 22,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 2,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 0,
"children" : [
{
"type" : "GEO_NEAR_2DSPHERE",
"works" : 22,
"yields" : 0,
"unyields" : 0,
"invalidates" : 0,
"advanced" : 2,
"needTime" : 0,
"needFetch" : 0,
"isEOF" : 0,
"children" : [ ]
}
]
}
]
}
}
It looks like you are using version 2.4 and are running into https://jira.mongodb.org/browse/SERVER-12231 which was fixed for version 2.6. If you upgrade, it's likely you will no longer see the bogus nscanned numbers in explain() output.