簡體   English   中英

Google Cloud Functions Firestore 限制

[英]Google Cloud Functions Firestore Limitations

我寫了一個 function,它在過去 24 小時內在 Firestore 中的所有更改文檔中獲取了一個 Querysnapshot。 我循環遍歷此 Querysnapshot 以獲取相關信息。 我想將此文檔中的信息保存到對每個用戶來說都是唯一的地圖中。 每個用戶平均每天生成 10 個文檔。 所以每個 map 平均被寫入 10 次。 現在我想知道整個事情是否是可擴展的,或者是否會達到 Firebase 中給出的每筆交易 500 次寫入限制,因為更多用戶將使用該應用程序。

我所說的限制記錄在 Google 文檔中

此外,我很確定我的代碼真的很慢。 所以我感謝每一次優化。

exports.setAnalyseData = functions.pubsub
  .schedule('every 24 hours')
  .onRun(async (context) => {

    const date = new Date().toISOString();
    const convertedDate = date.split('T');

    //Get documents (that could be way more than 500)
    const querySnapshot = await admin.firestore().collectionGroup('exercises').where('lastModified', '>=', `${convertedDate}`).get();
    //iterate through documents
    querySnapshot.forEach(async (doc) => {

      //some calculations

      //get document to store the calculated data 
      const oldRefPath = doc.ref.path.split('/trainings/');
      const newRefPath = `${oldRefPath[0]}/exercises/`;
      const document = await getDocumentSnapshotToSave(newRefPath, doc.data().exercise);

      document.forEach(async (doc) => {
        //check if value exists
        const getDocument = await admin.firestore().doc(`${doc.ref.path}`).collection('AnalyseData').doc(`${year}`).get();

        if (getDocument && getDocument.exists) {

          await document.update({
            //map filled with data which gets added to the exisiting map
          })
        } else {
          await document.set({
            //set document if it is not existing
          }, {
            merge: true
          });
          await document.update({
            //update document after set
          })
        }
      })
    })
  })

您問題中的代碼不使用 Firestore 上的交易,因此不受您引用/鏈接的限制的限制。

我仍然建議對您的查詢設置一個限制,並以合理的批次處理文檔(幾百個是合理的),這樣您就不會在代碼上施加不可預測的 memory 負載。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM