简体   繁体   English

我在 Firestore 中执行了多少次读取?

[英]How many reads in Firestore am I performing?

Say I have a collection called text_messages in firestore that has 500 documents in it, and I know the doc_id of one of those documents.假设我在 firestore 中有一个名为text_messages的集合,其中包含 500 个文档,并且我知道其中一个文档的 doc_id。

I want to execute the following code to fetch the data of that document down:我想执行以下代码来获取该文档的数据:

doc = db.collection("text_messages").document("my_doc_id").get()
data = doc.to_dict()

So my question is this: does this count as 1 read or 500 reads?所以我的问题是:这算作 1 次读取还是 500 次读取? I'm asking from a billing standpoint and want to optimize cost.我是从计费的角度询问并希望优化成本。

Since the accepted answer can be easily misunderstood, I am posting an alternative.由于接受的答案很容易被误解,因此我发布了一个替代方案。

Firestore charges for each document that is read for you on the server. Firestore 对在服务器上为您读取的每个文档收费。

Since your example reads only a single document, you will be charged for one document read no matter how many documents are in the collection that you read it from.由于您的示例只读取一个文档,因此无论您从中读取它的集合中有多少文档,您都需要为读取的一个文档付费。

Performing a query does not read every document that may match the conditions, but uses indexes for determining which documents match the conditions and then reads only those documents.执行查询不会读取每一个可能符合条件的文档,而是使用索引来确定哪些文档符合条件,然后只读取这些文档。

So in a Firestore query you get (at most) charged for the number of documents that the query returns, not for the number of documents that the database has to consider.因此,在 Firestore 查询中,您(最多)会为查询返回的文档数量付费,而不是为数据库必须考虑的文档数量付费。

Firebase counts as one read for each query, it doesn't matter if your collection has 500 docs or 5000 doc. Firebase 对每个查询计为一次读取,无论您的集合有 500 个文档还是 5000 个文档都没有关系。 A single query counts as one read, even if it had to go through 500 docs searching for it in the cloud firestore.单个查询算作一次读取,即使它必须通过 500 个文档在云 Firestore 中搜索到 go。

Also keep in mind that, while listening for a query, you are charged for a read each time a document in the result set is added or updated.另请记住,在侦听查询时,每次添加或更新结果集中的文档时,您都需要为读取付费。

// Reading a specific document,
db.collection("text_messages").document("my_doc_id")
    .get() // One read

// Using where query
db.collection("text_messages").where("sender", "==", "jack sparrow")
    .get() // One read

// Reading a whole collection
db.collection("text_messages")
    .get() // One read

You can read more here你可以在这里阅读更多

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

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