Firebase:减少大型集合的读取次数 [英] Firebase: Reduce number of reads for a large collection

查看:27
本文介绍了Firebase:减少大型集合的读取次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过大量搜索,在阅读firebase集合时,我发现很多不该做的事"(主要示例是3万美元的账单),但是我在努力寻找一个解决方案.解决我们问题的合适方法.

After a lot of searching, I've found a lot of "what not to do" when it comes to reading firebase collections (main example being the incident of the $30k bill), however I'm struggling to find a suitable solution for our problem.

我们的网站包含产品目录.该目录从名为"products"的firestore集合中填充,其JSON结构为:

Our website contains a product catalogue. The catalogue is populated from a firestore collection called 'products', with the JSON structure as:

"products"{
   "productId"{
      "name":
      "price":
      "brand":
       ...
  }
}

为了加载集合,我使用以下内容:

In order to load the collection, I use the following:

this.products = this.db.collection('products').valueChanges({idField: 'productID'});

在高峰时期,我们预计该产品系列将包含多达2000种产品.我担心的是,随着网站的扩展,这将变得极其昂贵.我注意到,每次浏览目录"页面时,它都会再次加载该集合.因此,以一个不太令人难以想象的示例为例,如果该页面一天被加载5000次并加载完整的集合(如当前那样),则将导致至少读取10,000,000个文档.

At its peak, we expect the collection to contain anywhere up to 2000 products. My concern is that as the website scales, this is going to become extremely costly. I've noticed that every time the 'catalogue' page is navigated to, it loads the collection again. Therefore, taking a somewhat not unimaginable example, if the page was loaded 5000 times in a day and loaded the full collection, as it currently does, it'll cause 10,000,000 document reads (at the very least).

我确定必须有更好的方法来执行此操作,但是,我似乎无法破解它.

I'm sure there must be a better way to do this, however, I can't seem to crack it.

任何建议都将以更好的方式来解决,并降低成本.

Any advice is greatly appreciated on a better way to approach this and keep the costs down.

推荐答案

您可以:

  1. 使用浏览器的本地存储来实现自己的本地缓存,并将其作为您更喜欢.
  2. 启用离线持久性,并将我们的查询定向到使用源选项进行缓存.
  3. 使用分页以避免加载整个立刻得出结果.
  1. Implement your own local cache using the browser's local storage and deal with that as you prefer.
  2. Enable offline persistence and direct our queries to the cache using source options.
  3. Use pagination to avoid loading the entire set of results at once.

这篇关于Firebase:减少大型集合的读取次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆