My web app deals with polls (surveys). Right now I have 2 tables as part of database schema.
polls
id
question
choices (ex: yes,no,maybe)
created
polls_responses
poll_id
user_id
tracker_id
response
The problem with this is that on some polls I have alot of responses (>1000). People can view the results of the polls and it will show how many users voted for yes, no, or maybe and how many anonymous users voted yes, no, or maybe. The problem with this is that whenever a user views the results of the poll, it has to loop through all the responses and count the total # of responses, # of responses for each choice, # of responses for each choice made by users, and # of responses for each choice made by tracker_id (anonymous users) and calculate percentages and display it in a bar graph. This makes the page load really slow. I was thinking of denormalizing the database to increase performance so that we have something like this
polls
id
question
choices (ex: yes,no,maybe)
total_responses (ex: 10,3,3,4)
user_responses (ex: 5,2,2,1)
anon_responses (ex: 5,1,3,1)
created
polls_responses
poll_id
user_id
tracker_id
response
That is, for the value in total_responses
, the 10 is the total, 3 is the # of responses for choice yes
, 3 is the # of responses for choice no
, and 4 is the # of responses for choice maybe
. The same format applies to user_responses
and anon_responses
fields. If I could get some opinions on this method, it would be greatly appreciated! Thanks for your time.
EDIT: I'm using MySQL