Suppose I have 50 objects in an array and each object has the property cost. Also, all of the objects are in a MySQL database.
Would it be more efficient to loop through all of the objects and adding up their cost member, or make a query such as SELECT sum(cost) FROM ...?
EDIT: A PDO database object has already been established.
In general, it’s far more efficient to have MySQL do it for you. However, if it really is just 50 objects, other factors such as network connection to the database may come into play. However, it’s never a good idea to write code that assumes the application will not scale; so use MySQL 🙂