Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

2dsphere vs 2d index performance

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?

like image 676
user1055568 Avatar asked Jan 20 '14 22:01

user1055568


People also ask

What is 2dsphere index?

A 2dsphere index supports queries that calculate geometries on an earth-like sphere. 2dsphere index supports all MongoDB geospatial queries: queries for inclusion, intersection and proximity. For more information on geospatial queries, see Geospatial Queries.

What is geospatial index in MongoDB?

MongoDB's geospatial indexing allows you to efficiently execute spatial queries on a collection that contains geospatial shapes and points.

What is the preferred format to store geospatial data in MongoDB?

In MongoDB, you can store geospatial data as GeoJSON objects or as legacy coordinate pairs.

What is 2d sphere?

a 2-sphere is an ordinary 2-dimensional sphere in 3-dimensional Euclidean space, and is the boundary of an ordinary ball (3-ball). a 3-sphere is a 3-dimensional sphere in 4-dimensional Euclidean space.


Video Answer


2 Answers

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.

like image 136
Stennie Avatar answered Nov 01 '22 16:11

Stennie


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,

like image 39
phil Avatar answered Nov 01 '22 15:11

phil