简体   繁体   中英

MongoDb geoIntersects doesn't use IndexOnly

I have a collection called search2 with about 20000 documents like this:

    {
        "loc": {
        "type": "Polygon",
        "coordinates": [
            [
            [
                43.78526674007639,
                11.14739998758569
            ],
            [
                43.78526674007639,
                11.183372851822439
            ],
            [
                43.79443488391605,
                11.183372851822439
            ],
            [
                43.79443488391605,
                11.264311796355125
            ],
            [
                43.812771171595415,
                11.264311796355125
            ],
            [
                43.83110745927479,
                11.264311796355125
            ],
            [
                43.83110745927479,
                11.273305012414314
            ],
            [
                43.849443746954144,
                11.273305012414314
            ],
            [
                43.858611890793824,
                11.273305012414314
            ],
            [
                43.858611890793824,
                11.264311796355125
            ],
            [
                43.8769481784732,
                11.264311796355125
            ],
            [
                43.8769481784732,
                11.246325364236752
            ],
            [
                43.88611632231286,
                11.246325364236752
            ],
            [
                43.88611632231286,
                11.237332148177565
            ],
            [
                43.895284466152546,
                11.237332148177565
            ],
            [
                43.895284466152546,
                11.228338932118376
            ],
            [
                43.904452609992234,
                11.228338932118376
            ],
            [
                43.904452609992234,
                11.165386419704065
            ],
            [
                43.895284466152546,
                11.165386419704065
            ],
            [
                43.895284466152546,
                11.156393203644878
            ],
            [
                43.88611632231286,
                11.156393203644878
            ],
            [
                43.8769481784732,
                11.156393203644878
            ],
            [
                43.858611890793824,
                11.156393203644878
            ],
            [
                43.849443746954144,
                11.156393203644878
            ],
            [
                43.849443746954144,
                11.165386419704065
            ],
            [
                43.83110745927479,
                11.165386419704065
            ],
            [
                43.83110745927479,
                11.156393203644878
            ],
            [
                43.812771171595415,
                11.156393203644878
            ],
            [
                43.812771171595415,
                11.14739998758569
            ],
            [
                43.79443488391605,
                11.14739998758569
            ],
            [
                43.78526674007639,
                11.14739998758569
            ]
            ]
        ]
        },
        "docId": 1,
        "docVote": 0,
        "title": "title-1",
        "_id": {
        "$oid": "5248725d2dd5622510000001"
        }
    }

I define an index with this command:

    db.search2.ensureIndex({"loc":"2dsphere"});

On the collection there are only this index and the default index on "_id" field.

When i execute the following query i expect that indexOnly parameter from the explains to be set true:

    db.search2.find({
        loc: {
        $geoIntersects: {
            $geometry: {
            type: "Polygon",
            coordinates: [
                [
                    [43.7269795, 11.1540365],
                    [43.7269796, 11.1540365],
                    [43.7269796, 11.1540366],
                    [43.7269795, 11.1540366],
                    [43.7269795, 11.1540365]
                ]
            ]
            }
        }
        }
    }, {
        loc: 1,
        _id: 0
    }).hint({"loc":"2dsphere"}).explain()

But this is the result:

    {
        "cursor" : "S2Cursor",
        "isMultiKey" : true,
        "n" : 14,
        "nscannedObjects" : 14,
        "nscanned" : 186,
        "nscannedObjectsAllPlans" : 14,
        "nscannedAllPlans" : 186,
        "scanAndOrder" : false,
        "indexOnly" : false,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "millis" : 20,
        "indexBounds" : {

        },
        "nscanned" : 186,
        "matchTested" : NumberLong(80),
        "geoTested" : NumberLong(80),
        "cellsInCover" : NumberLong(1),
        "server" : "*******"
    }

I notice that isMultiKey is true, caused by documents polygon syntax. Cursor is S2Cursor so i suppose that the index was used. But why indexOnly is false? Is due to the polygon syntax? so is impossible to have IndexOnly=true ? Thanks in advance

indexOnly explanation from here states:

indexOnly is a boolean value that returns true when the query is covered by the index indicated in the cursor field. When an index covers a query, MongoDB can both match the query conditions and return the results using only the index because:

  • all the fields in the query are part of that index, and

  • all the fields in the query are part of that index, and

And from here it states:

An index cannot cover a query if:

  • any of the indexed fields in any of the documents in the collection includes an array. If an indexed field is an array, the index becomes a multi-key index index and cannot support a covered query.
  • any of the indexed fields are fields in subdocuments.

Hope it helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM