简体   繁体   English

Firebase:以优化的方式存储数据以减少写入和读取次数

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

I am developing an Uber Eats like app for a project.我正在为一个项目开发类似 Uber Eats 的应用程序。 Clients(restaurants) add Menus and menu items(products) into the cloud firestore database.客户(餐厅)将菜单和菜单项(产品)添加到云 Firestore 数据库中。 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?我担心的是,当产品集合随着时间的推移变得越来越大(预计多达 15000 种产品)时,查询时间和写入和读取计数 go 会增加并减慢应用程序吗?

What would be the best way to store products, which can be efficiently queried by its location?存储产品的最佳方式是什么,可以通过其位置有效地查询?

Firebase > Documentation > Firestore > Build > Geo queries Firebase > 文档 > Firestore > 构建 > 地理查询

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. Cloud Firestore 只允许每个复合查询使用一个范围子句,这意味着我们不能通过简单地将纬度和经度存储为单独的字段并查询边界框来执行地理查询。

Solution: Geohashes Geohash is a system for encoding a (latitude, longitude) pair into a single Base32 string.解决方案:Geohashes Geohash 是一个将(纬度、经度)对编码为单个 Base32 字符串的系统。 In the Geohash system the world is divided into a rectangular grid.在 Geohash 系统中,世界被划分为一个矩形网格。

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:安装帮助程序库创建和解析 Geohashes 涉及一些棘手的数学问题,因此我们创建了帮助程序库来抽象 Android、Apple 和 Web 上最困难的部分:

Firestore query performance will remain same irrespective of number of documents your collection has.无论您的集合拥有多少文档,Firestore 查询性能都将保持不变。 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.获取 20-30 页的文档应该很好,除非用户要求更多结果,否则不会加载更多文档。

Also, checkout How do queries work in Cloud Firestore?另外,请查看 Cloud Firestore 中的查询如何工作?


For GeoQueries, see How to run a geo "nearby" query with firestore?对于 GeoQueries,请参阅如何使用 firestore 运行地理“附近”查询?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM