should this database table be normalized?

允我心安 提交于 2019-12-07 08:02:30

问题


i have taken over a database that stores fitness information and we were having a debate about a certain table and whether it should stay as one table or get broken up into three tables.

Today, there is one table called: workouts that has the following fields

id, exercise_id, reps, weight, date, person_id

So if i did 2 sets of 3 different exercises on one day, i would have 6 records in that table for that day. for example:

id, exercise_id, reps, weight, date, person_id
1, 1, 10, 100, 1/1/2010, 10
2, 1, 10, 100, 1/1/2010, 10
3, 1, 10, 100, 1/1/2010, 10
4, 2, 10, 100, 1/1/2010, 10
5, 2, 10, 100, 1/1/2010, 10
6, 2, 10, 100, 1/1/2010, 10

So the question is, given that there is some redundant data (date, personid, exercise_id) in multiple records, should this be normalized to three tables

WorkoutSummary:
- id
- date
- person_id

WorkoutExercise:
- id
- workout_id (foreign key into WorkoutSummary)
- exercise_id

WorkoutSets:
- id
- workout_exercise_id (foreign key into WorkoutExercise)
- reps
- weight

I would guess the downside is that the queries would be slower after this refactoring as now we would need to join 3 tables to do the same query that had no joins before. The benefit of the refactoring allows up in the future to add new fields at the workout summary level or the exercise level with out adding in more duplication.

any feedback on this debate?


回答1:


Don't assume that queries will be slower after normalizing. Joins on a small number of tables are very cheap if the tables are indexed properly.

On the other hand, queries on a non-normalized table can easily end up being much slower. For example, in your original schema, simply trying to query the distinct dates on which a workout was done is far more expensive than it would be with the normalized version.

Definitely normalize it at this point. If you run into performance issues later on, then you can start selectively denormalizing certain parts of the data in addition to the already-normalized schema. But in all likelihood you will never reach that point with a small database.




回答2:


The new refactoring seems good, and performance will not be all that affected if you have the appropriate indexes on the various tables. (Indexes can be created on all foreign keys)

So YES, that seems like a perfectly normal refactoring.



来源:https://stackoverflow.com/questions/2617711/should-this-database-table-be-normalized

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!