I hope this doesn't sound too petty and cause opinionated answers.
On my page I've already queried my database for some values with counts and stored them in an array for formatting later on my page. The topicCount is a count of articles posted in each topic.
topicName | topicCount
--------------|-------------
Latest News | 101
Events | 273
...
I've now decided to display a count for all articles. Now, this is what I was going to do:
SELECT COUNT(*) AS articleCount FROM blogArticles
$articleCount = $row['articleCount'];
But I'm now curious whether running a count on my array would be quicker and/or less expensive, seeming as I've already got the data in my script.
$articleCount = 0;
foreach ($results as $key=>$value) {
$articleCount + $value['topicCount'];
}
Which method is better and why or is there too little difference to care? Is reducing 20,000 SQL requests per day to get a count much of a save?
If I was just counting array items count($array) I'd probably take a guess that counting the array was much quicker/less expensive than querying the database - but having to add up totals makes me think there could be a difference, especially if I had 1000 topics with counts.