有没有办法加快此Firebase云功能的执行时间?

时间:2019-05-27 02:48:37

标签: firebase-realtime-database google-cloud-functions

我有以下firebase云功能(js sdk),可以正确执行。我想知道是否有一种方法可以加快执行时间。带有10个项目的userGroups集合平均需要花费250ms。我希望使用arround 500个项目的userGroups集合运行此功能。任何帮助表示赞赏!

.ref('users/{userId}/mood/')
.onUpdate((change, context) => {
  const userId = context.params.userId
  const beforeData = change.before.val(); 
  const afterData = change.after.val(); 
  const afterDataValue = parseInt(afterData.value || 0);
  const afterDataElement = afterData.element;
  const beforeDataValue = parseInt(beforeData.value || 0);
  const beforeDataElement = beforeData.element;

  const userGroups = admin.database().ref('/users/' + userId + '/groups/location/Items');

  const groupMood = userGroups.once("value")
  .then(function(snapshot) {
    snapshot.forEach(function(childSnapshot){
      const itemId = childSnapshot.key
      const currentElementValueRef = admin.database().ref('/groups/location/Items/' + itemId + '/mood/' + afterDataElement + '/value');
      const currentElementVoteCountRef = admin.database().ref('/groups/location/Items/' + itemId + '/mood/' + afterDataElement + '/votecount');


      if(beforeDataElement){
        const previousElementValueRef = admin.database().ref('/groups/location/Items/' + itemId + '/mood/' + beforeDataElement + '/value');
        const previousElementVoteCountRef = admin.database().ref('/groups/location/Items/' + itemId + '/mood/' + beforeDataElement + '/votecount');

        previousElementValueRef.transaction(value => {
          return (parseInt(value || 0) - beforeDataValue>0?parseInt(value || 0) - beforeDataValue:0)
        })

        currentElementValueRef.transaction(value => {
          return parseInt(value || 0) + afterDataValue
        })

        previousElementVoteCountRef.transaction(votecount => {
          return (parseInt(votecount || 0) - 1>0?parseInt(votecount || 0) - 1:0)
        })

        currentElementVoteCountRef.transaction(votecount => {
          return parseInt(votecount || 0) + 1
        }) 

      } else {     
        currentElementValueRef.transaction(value => {
          return parseInt(value || 0) + afterDataValue
        })

        currentElementVoteCountRef.transaction(votecount => {
          return parseInt(votecount || 0) + 1
        })

        const voteCountRef = admin.database().ref('/groups/location/Items/' + itemId + '/mood/votecount');

        voteCountRef.transaction(votecount => {
          return parseInt(votecount || 0) + 1
        })  
      } 
    })
  })
  return groupMood })

1 个答案:

答案 0 :(得分:0)

好像您正在循环内从数据库中读取。如果我不得不猜测,那就是大部分函数时间都花在了这里。三个想法:

  1. 在执行其他任何操作之前:您应该检查一下实际花费的时间,而不是依靠我的直觉。您可以使用https://cloud.google.com/trace/或直接在代码中添加一些console.log()语句。

  2. 函数启动时,您可能会将整个/groups/location/Items/集合读入变量。然后,您的代码将仅从循环中的该变量读取(快速),而不是从数据库中读取(缓慢)。仅当/groups/location/Items/集合足够小以适合内存时,这才有可能。

  3. 与运行上述一项繁重的工作不同,您可以在每次/users/***/groups/location/Items更新时进行一次小的计算,并将其存储在数据库的节点中。我不完全了解您的代码的功能,但是过去许多小计算而不是一个大计算的模式对我来说很有用。

祝你好运!