parse-community / parse-php-sdk

The PHP SDK for Parse Platform
https://parseplatform.org/
Other
811 stars 346 forks source link

How to get data from relation without doing fetch? #411

Closed hamzabinamin closed 6 years ago

hamzabinamin commented 6 years ago

Issue Description

~~ Please put your description here. Also indicate what's happening vs. what you expect to happen ~~

I have two tables named as Ingredient and Amount. Each ingredient has a relation column called amounts, that can either contain one or more than one amount objects. What I am trying to do is to fetch all ingredients in a loop and then fetch the amounts for all of those ingredients as well. However I have noticed that fetching amounts like that inside a loop slows down fetch a lot and I am not able to get results quickly.

As a reference I have 900+ ingredients and the server times out if I fetch all of the 900 ingredients, even if I fetch around 200, it takes around 2 minutes and 14 seconds to fetch them all with their amounts. Is there a way that I can fetch all the amounts for a certain ingredient without doing a second for-loop? Ultimately what I would want is to return all ingredients along with the amounts that they have.

Here's the code

try { 
$query = new ParseQuery("Ingredient"); 
$query->descending("name"); 
$query->limit(1000); 
$results = $query->find();
for ($i = 0; $i < count($results); $i++) { 
$object = $results[$i]; $ingredient = new Ingredient($object->getObjectId(), $object->get('name'), $object->get('category'), $object->get('KH'), $object->get('EW'), $object->get('FE'), $object->get('ALK'), $object->get('amounts')->_encode()); 
array_push($old_values_array, $ingredient);
$relation = $object->get('amounts');
$query1 = $relation->getQuery();

$results1 = $query1->find();

for ($j = 0; $j < count($results1); $j++) { 
    //echo "Got here in loop 2";
    $object2 = $results1[$j];
    $id = $object2->getObjectId();
    $name = $object2->get('name');
    $grams = $object2->get('grams');

    $amount = new Amount($id, $name, $grams);
    array_push($amounts_array, $amount);  
 }   
}
hamzabinamin commented 6 years ago

@tmc @jhansche @lacker @hramos @noughts

flovilmart commented 6 years ago

Closing as unrelated to the php SDK.

Please don’t at people like that this is rude and un welcomed.

Check your indexes in your database and your slow queries in mongodb.

hamzabinamin commented 6 years ago

@flovilmart I'm sorry for tagging everyone but I submitted a thread a month back and no one relied on that.

I know this isn't a bug bug I would like to know what's the best way to retrieve data from a relation in a situation like this?

  1. Is it somehow possible to fetch objects from a relation object without doing the find query and iterating over all of the objects?
  2. Or can I get just the Object IDs of the objects directly from the relation object?
flovilmart commented 6 years ago

Nobody replied likely because this is not a bug.

You should really consider stackoverflow or stackexchange for those questions.

Last thing, check your indexes