Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
Ok, so I've been thinking a lot about various sites like Reddit.com. There are thousands of posts, and for each post thousands of comments and on top of all that there are votes which are tracked by user for all comments and posts.
So, considering articles, comments, and article votes (don't really care about comment votes) the way I know how to do it would be 3 tables:
Articles:
id, value, username, totalvotes, other relevent data
Comments:
id, articleid, value, username, other relevent data
votes:
id, articleid, username, votevalue (+1,-1), other relevent data
So basically a one to many relation between Articles and comments/votes. Here are the questions I have in regards to this:
Is this the right way to do this?
Wouldn't it be extremely slow to tally all the votes by iterating through the whole votes table looking for the right article?
Would you keep a running total going or just query the whole votes table everytime (question 2).
My other idea was to make tables on the fly for each article, but that might be overkill. Thoughts?
yes
no - indexing is your friend
no - that is denormalized and would be hard to maintain
oooh.. no
One answer : Cache !
As Randy said this is not a big deal to have a lot of datas if your tables are well indexed,
but it can be too slow for a good user experience. So consider to cache everything you can (like number of responses) and update this only when a new comment is made.
Also I strongly advice you to fetch all related datas that aren't in cache by ajax and not directely at page load.
TLDR :
1. Yes
2. No
3. Cache
4. Hell no
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm building a website that contains articles, these articles won't be shown until the user is logged in, I'm planning to add two buttons, the first one is called "follow this article", the second is meant to make the article as a favourite. My question is how can I structure that in my database?
Should I just add a table that contains id_article, id_user, followed(1 or 0) and favourite (1 or 0), or is there another solution that would let me economize more memory space?
NOTE: My website will have lots of articles that will be visited by a lot of users.
Make two separate relations: articles_followed (id, id_article, id_user) and articles_favourite (id, id_article, id_user). This will give you enough flexibility in:
adding new attributes, specific for each relation,
sharding your database (at some point): it'd be easier to move one relation from another since they are not depending on each other,
concurrent update: you can use separate locks on these two, in addition, you'll have fewer issues with transaction visibility,
you don't need to keep is_followed or is_favourite flag: if there's an entry, then it means that article is followed (once unfollowed, simply remove the line)
Think about your domain classes structure -- which option would be more convenient to code? I bet, in case of two separate relations, it would be clearer.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am creating a website where users can easily calculate the calories they eat and see the repartition in term of fat, carbo, etc.
I want the users to be able to retrieve data from previous days.
I then need to store the data sent by my users everyday (basically, they input how much of each food they have eaten everyday and I am making the calculation then store the results).
The question if the following: what would be the best way to store the data? I have to store the data for each user for each day. I can't think of a simple solution (I think creating a new table for each new day would not be great, would it?).
I'm using PHP and MySQL for now.
Thanks for the help!
It seems that you are a step ahead of your self with the daily breakdown question.
First, you need to decide what you need to store, e.g. fields and normalise the way they are stored.
For example, you would have the following tables:
Users:
Id
..
EatItems:
UserId
ProductId
Calories
Fat
DateTime
Once you have these tables up and running, you can build reporting layer on top of that to breakdown consumption by user / date or anything else you might be interested in.
You could have a table that holds the input/calculated data/date which relates to a user/account.
When the user views previous day's, select the data that relates to that user.
I wouldn't create a table for each day. One table would suffice.
However, I would suggest attempting something and posting the code for specific issues you have if you run into before posting here.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
Wish you a peaceful and healthy new year. **
I am working on a survey database design for mysql/php/wordpress for estimated 10,000,000 users. Each user will eventually answer about 5,000 questions over a course of several years. These questions are answered mainly as scale of : AGREE, NEUTRAL, DISAGREE, DONT KNOW as multiple choice answers. There is no right or wrong answer. A user would be able to attempt the questions again in the future. Also, at each attempt his/her answer_record gets updated with new data. Would the following database design be reasonable from database performance and data normalization perspective? Thank you in advance.
TABLE_USER:
user_id
username
user_email
[other user specific fields]
TABLE_QUESTION:
question_id
question_text
question_image
question_category1 [A question may exist in more than 1 category]
question_category2
question_category3
TABLE_ANSWER:
answer_id
user_id
question_id
answer_agree
answer_neutral
answer_disagree
answer_dontknow
answered_datetime
answer_number_of_attempts
Sincerely,
Harrison.
Part of proper db design means stepping back and making sure that if you added one more thing, you wouldn't have to redesign the tables, and also that discrete types of information are separated out. If several columns are doing the same thing (but recording different answers) you should split them out into another table and have a single link table.
Also, do you really need to say Table in the table name? of course it's a table, what else would it be?
TABLE_USER is fine
for TABLE_QUESTION you should drop the category columns, instead make a new table
TABLE_CATEGORIES
with information on the different categories
and have another table
CATEGORIES_PER_QUESTION
question_id
category_id
that allows a question to have any number of categories, you can look which ones each question has by querying categories_per_question
TABLE_ANSWER should be split into two tables,
RESPONSE
response_id
user_id
question_id
answer_id
datetime_responded
and ANSWERS
answer_id
answer_name
Where answer name is AGREE, NEUTRAL, DISAGREE, DONT KNOW or any other sort of answer you might provide.
If you wanted to be fancy, you could even have another join table between ANSWERS and TABLE_QUESTION, indicating what answers will be available per question.
To know the number of attempts, and other information I dropped, you can query the DB so it doesn't need a column for itself.
I realize you want help with DB design, but even if the design is perfect, this will fail to scale reasonable if your system is not planned properly for scaling (BIG).
A proper designed API can scale endlessly.
With those numbers it will be cheaper overtime to have this external and build an API for it so you can scale properly. Building something directly into WordPress will require you to scale to quickly in all directions just for running PHP, HTTP and MySQL.
If you build an API in between WordPress and your survey database you can then scale MySQL and build any number of systems in between, Memcache, search engines, etc...
This will give you better separation between your systems allowing for more efficient scaling.
Scaling each only when it needs it.
So I would plan your system/infrastructure at this point also.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I have a system where users can 'like' content. There will likely be many hundreds of these likes going on at once. I'd like it to be AJAX driven so you get an immediate response.
At the moment I have a mysql table of likes which contains the post_id and user_id and I have a 'cached' counter on the posts table with the total number of likes - simple so far.
Would I benefit in any way, from storing any of this information in mongodb to take the load off of mysql?
At the moment, I click like, and two mysql queries run - and INSERT into likes and an UPDATE on posts. If I'm in a large-scale environment in heavy read/write situation what would be the best way to go?
Thanks in advance :)
MySQL isn't a good option for something like this, as a large number of writes will cause scaling issues. I believe MongoDB's real advantage is schemaless JSON document oriented storage, and while it should perform better than MySQL (if set up correctly), I think you should look at using Redis to store counters like this (The single INC command to increase a number value is the cream on top of the cake). It can handle writes much more efficiently than any other database, as per my experience.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I need to build a website where users can upload their photos, and vote other people photo, like Facebook like
i was thinking the best ( or only way ) to store this info:
1 - i can just use a simple counter on photo records, and check in session if people would vote more than once ( vote are just for fun ) but on a new session i can vote the same photo again
2 - the other way is create a table where i store userid and photoid, this way user can't vote more than once, but i'm not sure if database grow could became an issue since table will grow larger and larger
3 - A text field on user record where i store a list of photo id, and i can build a session array on login or just build a PHP array on page load. This could be the best way, i guess i can have issue if i need to search or count on specific situations. LONGTEXT should contain 4Gb so i think i would never reach maximum lenght
I'm assuming your question is:"What is the best way?" I would say 2 would be the best since that's the easiest to check wether a person has voted yet. The table won't get too big. People are using tables also for registering users and those can be alot as well.
yes you can move forward with the idea of text field on user record to store comma or pipe separated photo id
or text field on the photo record to store comma or pipe separated user id who liked that photo.
Its much better than creating a relational table
Here what you are trying to do is trying to create a website like Mark Zuck created earlier (Facemash). http://www.facemash.com.au/
You can download this script from http://webtify.com/internet/facemash-clone-script/802/
Regarding your logic : I think that #3 is good for this script