We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

How many queries to save entire resultset

Hey,

I'm trying to reduce the number of DB queries that occur when performing an operation on every model of a resultset. If I iterate over a result and perform the same operation, does phalcon perform man small db queries, or does it create a larger one?

IE:

$results = Model::find($conditions);
foreach($results as $result) {
    $result->status = "changed";
    $result->save();
}

Or should i create a phql query to do this, or is there something else I should be doing to reduce the number of DB calls?



4.7k
Accepted
answer

Well- in that example u have 1 query to get results and 1 query for every result to save new status. I do not know what is best, depends of your need and project requirements. Consider situation when results is more than few lines- then maybe query will be better to do that job. But for me- i prefer simplies ways.

Good luck :)