0

I have the next collection sample:

var testSchema = new Schema({
title:          {type: String, required: true},
owner:          {type:Schema.Types.ObjectId}, 
locatedAt:      {type: {}, index: '2dsphere', sparse: true, "2dsphereIndexVersion": 2, required: true}
});

I inserted 10.000 rows for evaluate the schema performance

db.test.find({"locatedAt":{"$near":{"$geometry":{"type":"Point","coordinates":[2.240413,41.582159]}}}}).explain();

The result is the next one:

{
    "cursor" : "S2NearCursor",
    "isMultiKey" : false,
    "n" : 10000,
    "nscannedObjects" : 48846,
    "nscanned" : 48846,
    "nscannedObjectsAllPlans" : 48846,
    "nscannedAllPlans" : 48846,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 95,
    "indexBounds" : {

    },
    "filterSet" : false
}

I tried creating a "2d" index with the same result.

Summarizing in my opinion the query is scanning too much rows, what I'm doing wrong? Maybe the Schema definition?

Thank you!!

Iván Peralta
  • 851
  • 1
  • 9
  • 25
  • For geo indexes, the nscanned* number aren't documents. They represent something internal to the geo index which isn't necessarily important to know about - the important thing is that the number is still proportional to the amount of work, so it can be used to compare geo queries. The reported time is 95ms to get all 10000 docs, so I think the query performed fine (note: don't actually use the millis field in explain for timing info in general). – wdberkeley Sep 25 '14 at 15:43
  • Thanks for your comment. I continued today evaluating the information (learning the meaning of the 'explain' fields) and if you include a limit or constraint the nscanned data is the right one and the milis goes to 4. So, it was just a bad utilization of the find, it wasn't a problem with the filters of the schema definition. – Iván Peralta Sep 25 '14 at 18:31

1 Answers1

0

After evaluated the 'Explain' documentation and some topics in blogs and here I need to conclude that wasn't a index problem or schema definition problem.

If you include a limit constraint the nscanned object goes down from around 50k objects to 36 (limit 10) and the milis goes down to 4.

Iván Peralta
  • 851
  • 1
  • 9
  • 25