2dsphere vs 2d index performance
Asked Answered
N

2

9

I need to do fast queries to find all documents within a certain GPS radius of a point. The radius will be small and accuracy is not that critical, so I don't need to account for the spherical geometry. There will be lots of writes. Will I get better performance with a 2d index than a 2dsphere?

Nellynelms answered 20/1, 2014 at 22:28 Comment(0)
R
12

If you definitely don't need spherical geometry or more than one field in a compound geo index (see the notes on Geospatial Indexes in the MongoDB manual), a 2d index would be more appropriate. There will also be a slight storage advantage in saving coordinates as legacy pairs (longitude, latitude) rather than GeoJSON points. This probably isn't enough to significantly impact your write performance, but it depends what you mean by "lots of writes" and whether these will be pushing your I/O limits.

I'm not sure on the relative performance of queries for different geo index types, but you can easily set up a representative test case in your own dev/staging environment to compare. Make sure you average the measurements over a number of iterations so documents are loaded into memory and there is a fair comparison.

You may also want to consider a haystack index, which is designed to return results for 2d queries within a small area in combination with an additional field criteria (for example, "find restaurants near longitude, latitude"). If you are not fussed on accuracy or sorting by distance (and have an additional search field), this index type may work well for your use case.

Raina answered 21/1, 2014 at 1:9 Comment(2)
Thanks, it's what I thought, but some of the mongo docs make it sound like the 2d index is more of legacy provision.Nellynelms
Note: geoHaystack is deprecated in MongoDB version 5+Schock
W
4

2dsphere is now version 3 after MongoDB 3.2

2dsphere is better

data from https://jira.mongodb.org/browse/SERVER-18056

more details : https://www.mongodb.com/blog/post/geospatial-performance-improvements-in-mongodb-3-2

3.1.6 - 2dsphere V2

"executionTimeMillis" : 1875,

"totalKeysExamined" : 24335,

"totalDocsExamined" : 41848,

After reindex

3.1.6 - 2dsphere V3

"executionTimeMillis" : 94,

"totalKeysExamined" : 21676,

"totalDocsExamined" : 38176,

Compared to 2d

3.1.6 - 2d

"executionTimeMillis" : 359,

"totalKeysExamined" : 95671,

"totalDocsExamined" : 112968,

Weakness answered 20/9, 2017 at 7:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.