$geoWithin and MultiPolygon performance issues?

682 views
Skip to first unread message

Paul Traina

unread,
Feb 8, 2016, 1:51:07 PM2/8/16
to mongodb-user
I regularly run a query on a collection that has only 35,126,773 records in it... the query runs just fine, but seems to exponentially, not linearly, slow down, when I add more polygons to my MultiPolygon object...

Anyone else seeing this?

Each record has one or two points in it, and those points are correctly formatted geoJson records:

"point1" : {
"name" : "Point of Interest",
"loc" : {
"type" : "Point",
"coordinates" : [
-111.608229,
40.160374
]
}
},
"point2" : {
"name" : "Point of Interest2",
"loc" : {
"type" : "Point",
"coordinates" : [
-111.608129,
41.160374
]
}
}

I want to find out if there are any records in my collection contain a point in one of about 12 very small polygons, so my query looks something like this:

        for msg in self.plexts.find({
                '$and': [{'time': {'$gt': self.start}}, {'time': {'$lte': self.end}}],
                '$or': [{'point1.loc': {'$geoIntersects': polygons}},
                        {'point2.loc': {'$geoIntersects': polygons}}]
        }):

and polygons is a multipolygon object:

        poly = {
            "$geometry": {
                "type": "MultiPolygon",
                "coordinates": [
                    [
                        [
                            [ -121.90305233001709, 36.307448013593934 ],
                            [ -121.89914703369139, 36.30191447539646 ],
                            [ -121.8962287902832, 36.306790926477625 ],
                            [ -121.90305233001709, 36.307448013593934 ]
                        ]
                    ],
                    [
                        [
                            [ -123.0362319946289, 37.9919689390867 ],
                            [ -122.6817512512207, 37.882441088286214 ],
                            [ -122.68089294433594, 37.91996158152244 ],
                            [ -123.07682991027832, 38.35168605955513 ],
                            [ -123.0362319946289, 37.9919689390867 ]
                        ]
                    ],
...

everything is indexed correctly (time, point1 and point2).


Markus Thielsch

unread,
Mar 2, 2016, 5:23:05 PM3/2/16
to mongodb-user

Hi Paul,

If you are running anything below v3.2 then please note that MongoDB implemented improvements for geospatial queries in v3.2 especially when searching in dense data fields.

If you are already running v3.2, you can check the following:

  • Check if your working set fits in your RAM - otherwise many random disk IO’s will occur which can be quite slow when not using SSDs
  • Check if other operations/write locks are happening when running the query - db.currentOp()
  • Looking at your query I can see that you are using the $or operator - one thing to keep in mind is that $or always does a collection scan unless both $or clauses are indexed - you can check by appending .explain() to your query what the winning strategy for MongoDB is to return your results
  • Whilst $geoIntersects does not require a geospatial index it’s performance will improve when using a geospatial 2dsphere index on the location field of your polygons

If you are still having issues, please provide the following:

  • MongoDB Version
  • The output of explain() from your query
  • The output from db.collection.getIndexes() relevant to the fields you are querying on

Regards,
Markus

Reply all
Reply to author
Forward
0 new messages