In my web application we have built a message center / inbox functionality, in the navigation of each page we link to the "Message Center" and include next to it a count of the unread messages, for example "Message Center (2)". To populate the (2), with each request we run a *SELECT COUNT(*) FROM MessageTable WHERE unread = true blah blah to get the count to include in the navigation.
This means that for each page load we are hitting this table and querying for a count.
I've come up with two alternatives:
- Keeping the aggregate count elsewhere in the database rather then calculating the count each time (also taking the load off the message table). This still means hitting the database w/ each request.
- Setting up a temporary queue destination per logged in user and sending updates to this queue if the user is logged in which can increment / decrement a count stored in the session. (not sure if this is even possible)
Are these good alternatives, are there other options?