Topic: Counting performance

Which of the following option is more performatic? Keep incrementing a count attribute in the database (e.g.: @community.members_count) or simply doing a SQL count (e.g.: @community.members.count) ?

Re: Counting performance

It really depends on how often you read the count vs. how often you need to update it. If you have an appropriate index on members the count(*) should be cheap; if you are doing a tablespace scan to find it, then it won't be.

The first is more complex to code and maintain. I'm always of the opinion that you should design something cleanly (in this case the SQL count), and see how it goes. If you find you have a performance bottleneck, then fix it by changing your design. Don't try to anticipate your performance problems in advance, you will almost always get it wrong, making extra complexity where you don't really need it, and still ending up with a problem where you least expected it...