I have a small application where a user logs in with a username/password, upon successfully logging in a set of navigation links are displayed on the left of the screen.
These links interface with the different options available in the application, however each link displayed may have a COUNT() value associated with it. For instance one link may be MESSAGES, this particular user may have 3 new messages in the messages.table, the next link may be TASKS, with 4 new tasks in the tasks.table.
I know I could perform a SQL count() on each of the desired tables but I am worried somewhat about the overhead of this (say I have to perform 20 different sql counts) on each page load to display these count numbers.
Has anyone run into this situation? Can your perform counts across multiple tables? One option that crossed my mind was storing these numbers in the users.table but this seems like allot of extra sql-footwork to keep the numbers synced...
COUNT(*) is usually very fast in that the table itself usually doesn't need to be accessed (the meta information in the database should already have stored the number of records in each table). If you say COUNT(fieldname), however, the actual table (or index) must be accessed to determine the number of rows without NULL in the fieldname column.
Using MAX() on a column that is incremented for each row can often do the trick very quickly so long as it is indexed. If you read stats more than you update, then you could benefit from updating stats in place whenever new data is added - triggers could be good for that, but be careful as excessive use of triggers can lead to performance issues. Alternatively you can just update stats once every X minutes - they won't be exact, but if you have lots of data then running stats in real time would cost too much.