在Firestore中获取查询的大小 [英] Get size of the query in Firestore

查看:64
本文介绍了在Firestore中获取查询的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从具有数千个文档的Firestore集合中有效地获取整个查询的大小?

How would one get whole query size efficiently from the Firestore collection which has thousands of documents?

就我而言,我通过一些不同的规则查询文档:

In my case I query documents by few different rules:

  • 开始日期
  • 结束日期
  • 地点ID
  • 关键字

然后我将查询限制为仅显示50条记录,但是我需要获得没有此限制的查询大小,因为这样分页才能在前端正确显示.

Then I limit the query to show only 50 records but I would need to get the size of the query without this limitations since that way pagination would show correctly in the front end.

我可以使用云功能,该功能与之前进行相同的查询,但没有限制,然后获取它的大小,但是有没有更有效的方法呢?查询大小可能是成千上万的文档,所以这样做会不会有性能问题?在这种情况下,计费如何运作?

I could use cloud function which makes the same query as earlier but without limit and then get size of it, but is there more efficient way of doing this? Query size could be thousands of documents so is there any performance issues by doing it this way? And how does the billing work on this kind of situation?

如果.我的查询是1500个文档,是否会有1500个读取操作才能获得此查询的大小?

If ie. My query is 1500 documents is there going to be 1500 read operations to get the size of this query?

还有其他一些主题建议使用计数器来获取集合的大小,但这不适合我的方法,因为大小取决于上述用户的搜索参数.

There has been other topics which recommends using counters to get size of the collection, but this does not suit my approach since the size depends on user's search parameters stated above.

欢迎对此问题提出所有建议!

All recommendations for this problem are welcome!

推荐答案

如果您在一个集合中有成千上万的文档,则可能可能需要经常更新计数器.在Cloud Firestore中,您只能每秒更新一次文档,这对于某些高流量的应用程序可能太低了.

If you have in one collection thousands of documents it might be possible to need to update a counter very often. In Cloud Firestore, you can only update a single document about once per second, which might be too low for some high-traffic applications.

查询大小可能是成千上万的文档,所以这样做会不会有性能问题?

Query size could be thousands of documents so is there any performance issues by doing it this way?

不,不会.根据有关 Firestore计数器的官方文档,您可以使用分布式计数器:

No, it won't. According to the official documentation regarding Firestore counter, you can use distributed counters:

要支持更频繁的计数器更新,请创建一个分布式计数器.每个计数器都是带有碎片"子集合的文档,计数器的值是碎片值的总和.

To support more frequent counter updates, create a distributed counter. Each counter is a document with a subcollection of "shards," and the value of the counter is the sum of the value of the shards.

这种做法可以帮助您实现所需的目标.

This practice can help you achieve what you want.

在这种情况下计费如何工作?

And how does the billing work on this kind of situation?

如果您想一次读取整个馆藏,则需要为每个读取的文档进行读取操作.

In case you want to read the entire collection at once, you'll be billed with a read operation for each document read.

我的查询是1500个文档,是否需要进行1500次读取操作才能获得此查询的大小?

My query is 1500 documents is there going to be 1500 read operations to get the size of this query?

如果要遍历整个集合以获取文档数,是的.

If you are looping the entire collection to get the number of documents, yes.

有关存储计数器的更多详细信息,请从此

For more details about storing counters, please see the last part of my answer from this post:

作为个人提示,请勿将这种计数器存储在Cloud Firestore中,因为每次您增加或减少计数器时,都会花费readwrite操作.将计数器免费托管在Firebase Realtime数据库 中.

As a personal hint, don't store this kind of counters in Cloud Firestore, because every time you increase or decrease the counter will cost you a read or a write operation. Host this counter in Firebase Realtime database at no cost.

这篇关于在Firestore中获取查询的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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