简体   繁体   English

高效SELECT的SQL表设计

[英]SQL Table Design For Efficient SELECT

Generic table setup to explain my structure - reduced to specify the core of my goals. 通用表格设置可以解释我的结构-简化以指定目标的核心。 Based after recipes. 根据食谱。

tbl_item tbl_item

item_id, item_name, misc_info item_id,item_name,misc_info
1, bread, etc. 1,面包等
2, cupcake, etc. 2,纸杯蛋糕等

tbl_ingredients_item tbl_ingredients_item

ii_id, ing_id, item_id ii_id,ing_id,item_id
1, 1, 1 1 1 1
2, 1, 3 2 1 3
3, 2, 1 3 2 1
4, 2, 4 4 2 4

tbl_ingredients tbl_ingredients
ing_id, ing_name ing_id,ing_name
1, Flour 1,面粉
2, Salt 2,盐
3, Butter 3,黄油
4, Frosting 4,结霜


I need to select every row of tbl_items, and its ingredients. 我需要选择tbl_items的每一行及其成分。 Then in php, iterate through each row so I can spit out the resulting statement: 然后在php中,遍历每一行,以便我可以吐出结果语句:

Item: bread 项目:面包
Misc Info: Etc. 其他信息:等等。
Ingredients: 配料:
1. Flour 1.面粉
2. Salt 2.盐

As it stands I believe the array will end up like this: 就目前而言,我相信该数组将最终像这样:

bread, etc., flour 面包等面粉
bread, etc., salt 面包等盐
cupcake, etc., flour 纸杯蛋糕等面粉
cupcake, etc., frosting 纸杯蛋糕等,糖霜

Is this the proper table setup, and what is an efficient way of dealing with this in php? 这是正确的表设置,并且在php中处理此问题的有效方法是什么?

Thanks for any help. 谢谢你的帮助。

Make the database structure simple and intuitive. 使数据库结构简单直观。 What you've got is fine. 您所拥有的一切都很好。

If you want to read out the recipes in one DB call, your options are to write a SQL query that joins your 3 tables, or write a view over your query and select from that. 如果要在一个数据库调用中读出配方,则可以选择编写一个将3个表连接起来的SQL查询,或者在查询中编写视图并从中选择一个视图。 If that isn't fast enough (and your DB supports it), you can create your view and index it, so that the denormalised data is persisted on disk (thus avoiding the joins). 如果那还不够快(并且您的数据库支持它),则可以创建视图并为其建立索引,以便将非规范化的数据保留在磁盘上(从而避免了联接)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM