I have a simple question yet I couldn't find a direct answer/explanation. Sorry if duplicate
I want to build a score table for a small quiz game where users get questions and they can either be right or wrong. So, I figure I have 2 choices, either a table with 1 row = 1 user answer, like this:
userId | rightAnswer | wrongAnswer |
---|---|---|
1 | 0 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
... | ... | ... |
Or a table with 1 row = 1 user global score:
userId | rightAnswers | wrongAnswers |
---|---|---|
1 | 42 | 21 |
2 | 100 | 0 |
3 | 12 | 13 |
... | ... | ... |
I have pretty much 0 experience with database/sql optimization, so i don't know which one is the most efficient.
Here is my thought process:
First option: Better(?)/Faster(?) to add/update a score because I don't care about the current table state, I just have to insert a row But, to get a user's global score I would need a heavy(?) query like
SELECT SUM(right), SUM(wrong) WHERE userId = x;
Since I would want to show a user's global score on each page/request, I feel like this is not the smart choice. Plus, since 1 row = 1 user answer, the table could get very very large.
First option: Slower(?) to add a score, since I would have to update an existing row. Meaning SELECT then UPDATE, on each answer. Not particularly my case, but this option is less flexible since - unlike the first option - I wont be able to store which question was rightly/wrongly answered. But, with this I can get a user's total score without a heavy query. Would be better if an UPDATE query could return the resulting row, so I don't have to UPDATE then SELECT to show.
Lastly, if you are kind enough to spend time answering me, could you explain briefly one or the other is technically better, and how could I test it (is there some sort of tool/procedure)
Thanks