views:

17

answers:

1

Hi, I'm working on a busy web site. Sometimes it gets much more traffic then avarage days. The content is cached but as site's owner want to see realtime post views it has to do at least one query to MySQL per post view which is become performance problem under heavy load.

All I can think of is create a different table for views and update original table regularly with a cron job. I'm sure this will reduce update time but I'm not sure if this is the best way.

A: 

Well, as for your question, there are three options:

  1. Don't cache that particular piece of information. But from your description, it sounds like you're doing that already and it's not working out.

  2. Use a write through cache. Whenever you "add" or "remove" a post, also generate the cached data for that item. This may be hard depending on how your caching system works, but it's an option.

  3. Invalidate the cache on write. Whenever to "add" or "remove" a post, remove the cached data for these posts. That way, on the next request for these from cache, the cache will be populated. This may or may not be easy, since it may not be trivial to detect what cached items reference a db node (It may require adding more information, or meta cache objects to keep track of this data).

Now, with that said, I find it kind of strange that one query is becoming a performance problem. What's more likely, is that the one query in question needs to be optimized, or your MySQL server needs to be optimized. I regularly see in excess of 5k queries per second on some of my production servers, and have no issues with load. That's because those queries are very efficient, and I have Query Caching turned on in MySQL (it's more effective than you would think)...

ircmaxell
Thanks for your reply. The query is very simple. It updates posts table to increase post views: "UPDATE posts SET views = views + 1 WHERE id='$postid'For the last part of your reply, I use query caching too. The problem is not about this single query. The server is very busy. And I'm trying to reduce MySQL load wherever possible. This one query is one of the most used queries. So optimizing it could be very helpfull.Thanks again.
redrain
Ohhh... It's a write query that's causing you trouble. That's quite different than the implication that I saw. What engine is the table using (InnoDB or MyISAM)?
ircmaxell