简体   繁体   English

Firebase 文档读取优化

[英]Firebase document read optimization

I'm using firebase firestore for my react-native app, I'm creating a app that will send user geolocation to firestore and generate heatmap, and the app will send user's location every 5 mins, my data looks like this我正在为我的 react-native 应用程序使用 firebase firestore,我正在创建一个应用程序,它将用户地理位置发送到 firestore 并生成热图,该应用程序将每 5 分钟发送一次用户的位置,我的数据如下所示

在此处输入图片说明

Right now I have about 1000 documents, every time I refresh the app, it will try to fetch all coords to generate the heatmap.现在我有大约 1000 个文档,每次刷新应用程序时,它都会尝试获取所有坐标以生成热图。

The problem I'm having is when it generate the heatmap, it will need to read all 1000 documents, what if I have 5000 coords/documents, and I have 10 users to use it, it will reach the documents read limit in firebase free plan which is 50k/day.我遇到的问题是当它生成热图时,它需要读取所有 1000 个文档,如果我有 5000 个坐标/文档,并且我有 10 个用户使用它,它将达到 firebase free 中的文档读取限制计划是 50k/天。

I know I can pay some money to increase the read limit, but just wondering if any one run into this and find optimize way to solve it.我知道我可以花一些钱来增加读取限制,但只是想知道是否有人遇到过这个问题并找到优化的方法来解决它。 Thanks!谢谢!

I don't know all the constraints of your application, but you could possibly store all the coordinates of one month in one document, in an array, reducing by 8928 the number of document reads.我不知道您的应用程序的所有限制,但是您可以将一个月的所有坐标存储在一个文档中,在一个数组中,将文档读取次数减少 8928。

If I did the maths corrrecly, based on this documentation page https://firebase.google.com/docs/firestore/storage-size which explains the Storage size calculations, you can calculate that a doc with 3 arrays named lat , long and ts under your coords collection which stores the data for 288*31 triplet values (288 = every 5 minutes in one day) will have a size of maximum 857,088 bytes, which is under the maximum possible size for a document (ie 1,048,576 bytes) as presented here: https://firebase.google.com/docs/firestore/quotas如果我正确地进行了数学计算,则基于此文档页面https://firebase.google.com/docs/firestore/storage-size解释了存储大小计算,您可以计算出具有latlong和 3 个数组的文档您的coords集合下的ts存储 288*31 个三元组值(288 = 一天中每 5 分钟)的最大大小为 857,088 字节,低于文档的最大可能大小(即 1,048,576 字节)为此处介绍: https : //firebase.google.com/docs/firestore/quotas

Of course, you'll have to deal with the array fields but for that you can use firebase.firestore.FieldValue.arrayUnion();当然,您必须处理数组字段,但为此您可以使用firebase.firestore.FieldValue.arrayUnion(); , see https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array ,见https://firebase.google.com/docs/firestore/manage-data/add-data#update_elements_in_an_array

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

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