【问题标题】:Firestore: How is read calculated?Firestore:如何计算读取?
【发布时间】:2021-11-22 21:36:30
【问题描述】:

数据结构:

Posts ( collection )

⤷ Post_1 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_1 ) ( field )

⤷ Post_2 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_1 ) ( field )

⤷ Post_3 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_1 ) ( field )

⤷ Post_4 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_2 ) ( field )

⤷ Post_5 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_1 ) ( field )

⤷ Post_100 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_1 ) ( field )

⤷ Post_1000 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_2 ) ( field )

⤷ Post_5000 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_1 ) ( field )

⤷ Post_10000 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_2 ) ( field )

⤷ Post_15000 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_2 ) ( field )

⤷ Post_20000 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_2 ) ( field )

⤷ Post_25000 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_1 ) ( field )

⤷ Post_50000 ( document )
  ⤷ post_id ( field )
  ⤷ post_description ( field )
  ⤷ category ( value = category_2 ) ( field )

问题1:假设用户发布了一个新帖子(帖子是post_45813),然后决定更新帖子描述,这种情况下的阅读量是如何计算的,会是多少?

问题2:如果我想从post_20000到post_20100获取文档而不考虑类别名称,这种情况下的read是如何计算的,会是多少?

问题 3:如果我想获取类别 1 内的所有文档,假设类别 1 内有 13833 个帖子,这种情况下阅读量是如何计算的,会是多少?

【问题讨论】:

    标签: firebase google-cloud-platform google-cloud-firestore


    【解决方案1】:

    您必须支付的读取次数始终等于查询返回的文档数。

    问题1:假设用户发布了一个新帖子(帖子是post_45813),然后决定更新帖子描述,这种情况下的阅读量是如何计算的,会是多少?

    发布新帖子的成本是一次写入操作,而更新帖子则涉及另一个写入操作。共 2 次写入。

    问题2:如果我想获取从post_20000到post_20100的文档,不考虑分类名称,这种情况下read是如何计算的,会是多少?

    如果 post_20000 和 post_20100 之间的文档数等于 100,那么您必须准确支付 100 次读取。

    问题 3:如果我想获取类别 1 内的所有文档,假设类别 1 内有 13833 个帖子,在这种情况下如何计算阅读量,会是多少?

    如果您的收藏包含 13833 个帖子,并且您想一次获得所有帖子,则必须支付 13833 次阅读费用。但请注意,这个世界上没有人会对阅读这么多帖子感兴趣。在这种情况下,您应该始终考虑以较小的块获取数据。这种技术称为pagination

    也可以根据某些字段考虑filtering the results

    还请注意,如果您的查询没有结果,根据有关Firestore pricing 的官方文档,它表示:

    查询最低收费

    对于您执行的每个查询,即使查询未返回任何结果,读取一个文档的最低费用也是如此。

    因此,例如,如果您尝试过滤 13833 条帖子但没有得到任何结果,您仍需支付 1 次阅读的费用。

    这里还有一篇您可能感兴趣的文章:

    【讨论】:

    • 关于第一个问题,当我们需要编写或更新特定文档时,Firestore 将首先读取所有文档以找到特定文档,因此需要读取,Firestore 是否这样工作?
    • 不,不会。编写文档时,没有涉及读取操作。显然,除非你明确地这样做。否则,不会发生读取操作。
    猜你喜欢
    • 2018-11-25
    • 2021-01-19
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-05
    • 1970-01-01
    相关资源
    最近更新 更多