Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix geospacial queries to use the MongoDB index #171

Merged

Conversation

remuslazar
Copy link
Contributor

Resolves #170

TOC

  1. Use the geospatial "2d" index for boundingbox queries
  2. Do not sort the result set when doing boundingbox queries for performance reasons

TL;DR

This changes the existing MongoDB geospatial index in "2d" and rework the query logic to use that.

Prior to this PR MongoDB was not able to use the geospatial index when doing boundingbox queries and this lead to a major performance degradation and high RAM usage on the MongoDB server instance. See #170 for details.

Technical Stuff

MongoDB uses the 2dsphere index only for queries with the $geometry operator, see https://docs.mongodb.com/manual/tutorial/query-a-2dsphere-index/. For "basic" $polygon queries only the legacy 2d index can be used.

To leverage the 2dsphere index a newer version of the MongoDB Library has to be used. The legacy version of the MongoDB driver (1.x) (currently used throughout the codebase) can not do that, unfortunately.

Benchmarks

On my local machine the query time for a boundingbox query went down from ~400ms to ~30ms. Using the index the mongo instance runs also fine with 512MB RAM in docker.

Sorting Behavior Change

Additionally this change also skips the sorting of the result set by ID when doing boundingbox queries to get even more performance. This is especially a thing when dealing with large result sets, e.v. 500 records or more.

Refactor both the nearby (radius) and the boundingbox queries to use the "2d" MongoDB index correctly.
Make the MongoDB index creation logic more robust and DRY.
See inline comments for details
We must do the sorting when NOT in boundingbox mode..
@remuslazar remuslazar changed the title Fix geospacial queries to use the 2s index Fix geospacial queries to use a 2d index Jan 31, 2021
@remuslazar remuslazar changed the title Fix geospacial queries to use a 2d index Fix geospacial queries to use the MongoDB index Jan 31, 2021
@webprofusion-chrisc
Copy link
Member

Thanks, this is awesome! Regarding the removal of the sort, we try to keep the results set the same between the SQL implementation and the MongoDB implementation so that we can automate a few tests but I'll merge this and see how I get on.

@webprofusion-chrisc webprofusion-chrisc merged commit f5bc875 into openchargemap:master Jan 31, 2021
@remuslazar remuslazar deleted the fix-geospacial-queries branch November 10, 2021 14:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bad API Query Performance (1000ms for a basic bounding box request)
2 participants