简体   繁体   English

如何在 Firestore 中更新 100 万个文档?

[英]How to update 1 Million documents in Firestore?

To facilitate reading, I made use of de-normalisation.为了方便阅读,我使用了去规范化。 This is how the workflow is.工作流程是这样的。

There are two collections有两个collections

  1. Users用户
  2. Events事件

Events has a start, end timestamps and status.事件有开始、结束时间戳和状态。 Start, End timestamps indicate when an event starts and ends Status maintains if its Upcoming or Live or Finished or Cancelled Status is updated by listening to every minute scheduler.开始、结束时间戳指示事件何时开始和结束如果通过侦听每分钟调度程序更新其即将到来或实时或已完成或已取消状态,状态将保持不变。

When a user registers for an event, I copy the event object under users/{user-id}/events.当用户注册一个事件时,我将事件 object 复制到 users/{user-id}/events 下。 This is required as I need to fetch what events a user registered.这是必需的,因为我需要获取用户注册的事件。

Problem问题

Consider there are 1 Million users subscribed for an event when the status is Upcoming.假设有 100 万用户订阅了状态为“即将到来”的事件。 When the status changes from Upcoming to Live, I need to update all documents under users/{user-id}/events collection, for all users.当状态从 Upcoming 变为 Live 时,我需要为所有用户更新 users/{user-id}/events 集合下的所有文档。

If i make sequential batch writes, it takes nearly 1000000/500 = 2000 batches and it takes nearly 15 to 30mins to update for one event change.如果我进行顺序批写入,则需要将近 1000000/500 = 2000 个批次,并且需要将近 15 到 30 分钟来更新一次事件更改。 I see it too much problematic as events increase.随着事件的增加,我认为它有太多问题。

I'm pretty worried about the limit of 10,000 updates to the whole firestore per second to use parallel batch writes.我非常担心每秒对整个 firestore 进行 10,000 次更新以使用并行批处理写入的限制。

How to handle this scenario so the the writes won't hit the limits and can be written as fast as possible?如何处理这种情况,以便写入不会达到限制并可以尽快写入?

If you know how fast your writes can be handled, you use Cloud Tasks to limit the rate of writes.如果您知道您的写入处理速度有多快,您可以使用 Cloud Tasks 来限制写入速率。 A full discussion is probably beyond the scope of a single Stack Overflow answer.完整的讨论可能超出了单个 Stack Overflow 答案的 scope。 After getting acquainted with Cloud Tasks , I suggest looking specifically at configuring a rate limit for the queue you will use to handle all the writes.在熟悉Cloud Tasks之后,我建议您专门查看为您将用于处理所有写入的队列配置速率限制 Tasks dispatched to a queue can invoke a function to perform the deletes.分配到队列的任务可以调用 function 来执行删除。

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

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