简体   繁体   中英

Firebase: Store data in an optimized way to reduce writes and reads count

I am developing an Uber Eats like app for a project. Clients(restaurants) add Menus and menu items(products) into the cloud firestore database. Users (app users who search for restaurants and menu items) can search for restaurants and menu items in their nearby locations. Currently I am saving all the products that clients are uploading in a single collection. (Products collection). When the user tries to search menu items by location I have used

.where("location", isEqualTo:location)

My concern is when the products collection gets bigger over time (expecting up to 15000 products) would the query time and write and reads count go up and slows down the app?

What would be the best way to store products, which can be efficiently queried by its location?

Firebase > Documentation > Firestore > Build > Geo queries

Cloud Firestore only allows a single range clause per compound query, which means we can't perform geo queries by simply storing latitude and longitude as separate fields and querying a bounding box.

Solution: Geohashes Geohash is a system for encoding a (latitude, longitude) pair into a single Base32 string. In the Geohash system the world is divided into a rectangular grid.

Install helper library Creating and parsing Geohashes involves some tricky math, so we created helper libraries to abstract the most difficult parts on Android, Apple, and Web:

Firestore query performance will remain same irrespective of number of documents your collection has. However, you should implement pagination in your app so all query results are not loaded at once (just like downloading a large file). Fetching documents in pages of 20-30 should be good and does not load further docs unless user asks for more results.

Also, checkout How do queries work in Cloud Firestore?


For GeoQueries, see How to run a geo "nearby" query with firestore?

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