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?