Verifying uniqueness on a batch save with a huge amount of objects - Parse Performance
Asked Answered
B

0

0

I'm doing a parse batch save requestas follows:

Parse.Object.saveAll(nameGenderArrayToSave)

nameGenderArrayToSave is an array with thousands of objects to save. I'm also interested in guarante the uniqueness of my data. So I have a beforeSave hook that do it for me:

Parse.Cloud.beforeSave("NameGender", function(request, response) {
    if (!request.object.isNew()) {
      // Let existing object updates go through
      response.success();
    }
    var query = new Parse.Query("NameGender");
    // Add query filters to check for uniqueness
    query.equalTo("name", request.object.get("name"));
    query.first().then(function(existingObject) {
      if (existingObject) {
        // Update existing object
        if (request.object.get("gender") != "U"){
            existingObject.set("gender", request.object.get("gender"));
        }
        return existingObject.save();
      } else {
        // Pass a flag that this is not an existing object
        return Parse.Promise.as(false);
      }
    }).then(function(existingObject) {
      if (existingObject) {
        // Existing object, stop initial save
        response.error("Existing object");
      } else {
        // New object, let the save go through
        response.success();
      }
    }, function (error) {
      response.error(error);
    });
});

The code above is running as expected but i'm with a performance problem since i'm triggering a find for each object i'm saving and so i'm reaching Parse request/sec limit. Could anyone help me here finding a way to solve this?

Berkly answered 17/11, 2015 at 16:40 Comment(2)
We do pretty much the same thing to ensure uniqueness, but through a cloud function... I think Parse's Cloud Code requests accounting for the /sec quota is a very stealthy cost, and most likely the reason most apps blow their free quota. Requests running on their own infrastructure should not be that expensive, and we don't use Cloud Code only for performance, we use it to save up on network requests!Rental
I'm guessing here, but it might be that existingObject.save() is triggering another beforeSave call. The documentation says you should call response.success(existingObject);Rental

© 2022 - 2024 — McMap. All rights reserved.