为什么这么多扫描对象?

时间:2014-06-10 06:53:49

标签: mongodb spatial sql-execution-plan

我有一个包含以下对象的表:

> 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 ] } }

以下查询的explain()给出了一个奇怪的(在我看来)结果:

> 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
}

为什么扫描这么多物体?我的意思是,桌子只有4个 物体和mongodb扫描22?

我很感谢任何解释。

再见,安德烈


> 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" : [ ]
                                        }
                                ]
                        }
                ]
        }
}

1 个答案:

答案 0 :(得分:0)

看起来您使用的是版本2.4并且正在运行https://jira.mongodb.org/browse/SERVER-12231,这是针对2.6版修复的。如果升级,很可能您将不再在explain()输出中看到伪造的nscanned数字。