1

I'm working on page with articles. We can vote on each article. I want to display all articles and above average voted articles on separate pages. My current scenario is something like this:

//For get all articles
GET ALL RECORDS FROM ARTICLES TABLE

//For above average articles
GET ALL RECORDS WITH VOTE ABOVE
    GET AVERAGE VOTE VALUE FROM ARTICLES TABLE
FROM ARTICLES TABLE

Which I find very inefficient. There are two events which changes average vote:

  • Somebody voted on an article
  • Somebody wrote an article

Is there any way I can track average vote value without querying database on every request? I guess I have to save state of how many votes has been sent and how many articles were written. Should I write those two vars in file, is this secure?

ewooycom
  • 2,651
  • 5
  • 30
  • 52
  • I would keep a sum and count in two separate columns as a denormalisation step; the average is trivial then. – Ja͢ck Feb 01 '13 at 13:21

1 Answers1

1

Just use your database as intended, it is as efficient as you're going to get. If you want to cut down on database traffic than use caching (Choosing a PHP caching technique: output caching into files vs. opcode caching) don't re-architect something that works and add needless complexity.

PHP doesn't have static variables that are persistent between requests, this makes an obvious / simple solution to the problem (you don't really have a problem, but your perceived inefficiency) impossible. Serializing variables to a file and having to read that file every request is not the way to go, file system IO is the least efficient construct.

Community
  • 1
  • 1
Louis Ricci
  • 20,804
  • 5
  • 48
  • 62