How expensive is it to find ('count') in CakePHP?

I have a CakePHP application where I am trying to calculate how many of each type of message a user has. I also use the paginate helper to display all user posts in my profile.
I could:
1) Make a request find('all', $conditions)for all user messages, then analyze it for the calculations I need (by sending a message by mail and checking if it matches what I'm looking for).
2) Make multipie requests find('count', $conditions), getting all the bills I need.
3) Maintain the new columns in the user table in order to keep track of these numbers when creating messages (this will be associated with the user account record when creating a new record).
Which one would be the best choice?

+3
source share
2 answers

Retrieving all the records from the database and counting them in PHP is very wasteful nonsense. It is for these databases.

A find('count')just makes a SQL query like SELECT COUNT(*) FROM … WHERE …, which is actually the fastest way. Add the appropriate conditions to get the right score, you may need to refresh your SQL to find out what conditions they are.

There is also a special counter-caching that you might want to examine if you consider hasMany a relationship.

+7
source

Source: https://habr.com/ru/post/1757249/


All Articles