mysql - How to optimize this database structure? -
for future project i'm planning i'll have 3 tables.
the 1st keeps track of recipe information, 2nd keeps track labels references recipe_id recipe table, 3rd table contains list of ingredients.
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 | +----------+------------+----------------------+
the recipe_ingredients references recipe_ingredients_label table, each ingredient goes under it's own label so:
pizza base contains 4 cups cheese, 3/4 cup white flour.
topping contains 1 cup mozzarela cheese, 1/2 cup tomato sauce.
the issue not recipes contains labels, i'd make labels optional based on recipe.
i thought of this:
creating recipe_id column in recipe_ingredients table, , during insertion database if no label found, reference recipe_id there instead so:
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 | +----------+------------+-----------------------------------+
is best way organize/optimize table performance , less data thrown on database? recommend?
Comments
Post a Comment