我应该如何构造Firestore文档以及每次都需要阅读的大量文档 [英] How should I structure Firestore documents with a large number of docs needed to be read each time

查看:119
本文介绍了我应该如何构造Firestore文档以及每次都需要阅读的大量文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务列表,每个服务都有许多结构如下的字段

I have a list of service that each have a number of fields that are structured like this

Service1 *

Service1*

     ServiceID*
     NetID*
     Country*
     price*
     xxx*
     xxx*
     10 fields in total*

Service2 *

Service2*

     ServiceID*
     NetID*
     Country*
     price*
     xxx*
     xxx*
     10 fields in total*

我目前已安排每个服务作为其自己的文档的位置,总计1,180个.但是,当用户访问我的站点并希望选择一项服务时,服务器将获取全部1180个文件.只有100个用户,firebase会读取10万个文档.我当时想将所有服务的副本保存为csv,并在每次用户想要搜索服务时从那里加载它.

I have arranged currently where each service is its own document for a total of 1,180. However when a user visits my site and wants to select a service, the server would fetch all 1180. With just 100users firebase would reading 100k docs. I was thinking of saving a copy of all the services as a csv and loading it from there every time a user wants to search the service.

是否有更好的结构化数据或使用其他方法来减少我对Firestore的读取次数.

Is there any better of structuring the data or using another method to reduce my read count of Firestore.

推荐答案

是否有更好的数据结构

Is there any better of structuring the data

没有其他更好的方法可以做到这一点.

There is no other better method for doing that.

但是,当用户访问我的网站并希望选择一项服务时,服务器将提取全部1180.

However when a user visits my site and wants to select a service, the server would fetch all 1180.

这不是与Cloud Firestore进行交互的正确方法.您正在以高昂的代价下载大量的数据.除此之外,用户永远不会一次需要所有这些数据量.一个解决方案是将数据加载到较小的块中.对于Android,建议您从以下文章中查看答案:

That's not a proper way to interact with Cloud Firestore. You are downloading huge ammout of data with a high cost. Beside that, a user will never need all that amount of data at once. A solution for that will be to load data in smaller chunks. For Android, I recommend you see my answer from the following post:

我当时想将所有服务的副本保存为csv,并在每次用户想要搜索服务时从那里加载它.

I was thinking of saving a copy of all the services as a csv and loading it from there every time a user wants to search the service.

没有必要. Firestore已经拥有自己的缓存机制.因此,一旦获得文档,就始终会从缓存中读取该文档,只要在服务器上,就不会进行任何新更改.这里有更多信息:

There is no need for that. Firestore already has its own caching mechanism. So once you get a document, it will always be read from cache, as long as on the server, no new changes are made. More infos here:

对于Web,默认情况下禁用离线持久性.要启用持久性,请调用enablePersistence方法.会话之间不会自动清除Cloud Firestore的缓存.因此,如果您的Web应用程序处理了敏感信息,请确保在启用持久性之前先询问用户是否在受信任的设备上.

For the web, offline persistence is disabled by default. To enable persistence, call the enablePersistence method. Cloud Firestore's cache isn't automatically cleared between sessions. Consequently, if your web app handles sensitive information, make sure to ask the user if they're on a trusted device before enabling persistence.

这篇关于我应该如何构造Firestore文档以及每次都需要阅读的大量文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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