如何优化这个数据库结构?

时间:2017-09-12 01:37:22

标签: mysql database database-design

对于未来的项目,我计划我有三张桌子。

第1个跟踪配方信息,第2个跟踪从配方表中引用recipe_id的跟踪标签,第3个表包含所有成分的列表。

recipe:
+---------+-----------------+-------------+
| id [AI] |  title          |  category   |
+---------+-----------------+-------------+ 
| 20      |  Italian Pizza  |   dinner    |
+---------+-----------------+-------------+
| 21      |  Greek Soup     |   lunch     |
+---------+-----------------+-------------+

recipe_ingredients_label:
+---------------+------------+-------------+
| label_id [AI] |  recipe_id |    label    |
+---------------+------------+-------------+ 
| 1             |  20        |  Pizza Base |
+---------------+------------+-------------+
| 2             |  20        |   Topping   |
+---------------+------------+-------------+

recipe_ingredients:
+----------+------------+----------------------+
| label_id |   amount   |   ingredients        |
+----------+------------+----------------------+ 
|    1     |   4 cups   |   Cheese             |
+----------+------------+----------------------+ 
|    1     |   3/4 cup  |   White Flour        |
+----------+------------+----------------------+ 
|    2     |   1 cup    |   Mozzarella Cheese  |
+----------+------------+----------------------+ 
|    2     |   1/2 cup  |   Tomato Sauce       |
+----------+------------+----------------------+

recipe_ingredients引用recipe_ingredients_label表,每个成分都在它自己的标签下面,如下所示:

披萨基地包含 4杯奶酪,3/4杯白面粉

Topping 包含 1杯莫扎里拉奶酪,1/2杯番茄酱

问题是并非所有食谱都包含标签,因此我想根据食谱使标签可选。

我想到了这个:

在recipe_ingredients表中创建一个额外的recipe_id列,如果没有找到标签,在插入数据库期间,只需引用recipe_id,如下所示:

recipe_ingredients:
+----------+------------+-----------------------------------+
| label_id |  recipe_id |   amount   |   ingredients        |
+----------+------------+-----------------------------------+ 
|    1     |    NULL    |   4 cups   |   Cheese             |
+----------+------------+-----------------------------------+
|    1     |    NULL    |   3/4 cup  |   White Flour        |
+----------+------------+-----------------------------------+ 
|    2     |    NULL    |   1 cup    |   Mozzarella Cheese  |
+----------+------------+-----------------------------------+
|    2     |    NULL    |   1/2 cup  |   Tomato Sauce       |
+----------+------------+-----------------------------------+
|   NULL   |     21     |   2 cups   |   Chicken            |
+----------+------------+-----------------------------------+
|   NULL   |     21     |   1/2 cup  |   Half carrots       |
+----------+------------+-----------------------------------+
|   NULL   |     21     |      2     |   Celery stalks      |
+----------+------------+-----------------------------------+

这是组织/优化此表的最佳方式,以提高性能,减少整个数据库中丢失的数据吗?你会推荐什么?

0 个答案:

没有答案