简体   繁体   English

MySQL-一个字段中有多个值或多对多关系?

[英]MySQL - multiple values in one field or many-to-many relationship?

Let's start with the context. 让我们从上下文开始。 I'm writing a small application for myself, which can be used to alter and create products in a MySQL database. 我正在为自己编写一个小应用程序,可用于在MySQL数据库中更改和创建产品。

Every product has at least one production method, for example 'forged', 'welded' or 'machined' (many more methods exist in my application). 每个产品都有至少一种生产方法,例如“锻造”,“焊接”或“机加工”(我的应用程序中存在许多种方法)。 The production method for a product can be any combination of these methods. 产品的生产方法可以是这些方法的任何组合。

The application currently has a dropdown menu with a list of checkboxes to choose the production methods from, and the production method is displayed by a textfield. 该应用程序当前具有一个下拉菜单,其中包含用于选择生产方法的复选框列表,并且生产方法由文本字段显示。 When my application displays the production method, I want these to be ordered alphabetically (not ordered by clicked order, like if you first clicked welded that will be the first value): 当我的应用程序显示生产方法时,我希望它们按字母顺序排序(而不是按单击的顺序排序,例如,如果您第一次单击“焊接”将是第一个值):

product table : 'forged - welded - machined' 产品 :“锻造-焊接-机械加工”
product chair : 'welded - forged - machined' 产品 椅子 :“焊接-锻造-机加工”
product chair : 'forged - welded - machined' 产品椅子 :“锻造-焊接-机加工”

When my application retrieves the product table I also want to set the 'forged', 'welded' and 'machined' methods checked to true. 当我的应用程序检索产品表时,我还想将选中的“伪造”,“焊接”和“加工”方法设置为true。

Should I use one field in the MySQL products table where a string of the combination of methods is saved (like above), and let my application handle the sorting when clicking the checkboxes and save this string to this one field in the products table, 我是否应该在MySQL产品表中使用一个字段,其中保存了方法组合的字符串(如上),并且让我的应用程序在单击复选框时处理排序并将该字符串保存到产品表中的这个字段中,
or, 要么,
should I use a many-to-many relationship in my database where a table of all production methods exists, and I use a junction table to connect product (pID = 2) with forged (mID = 1), welded (= 2) and machined (= 3) like 我应该在存在所有生产方法表的数据库中使用多对多关系,并使用联结表将产品(pID = 2)与伪造(mID = 1),焊接(= 2)和加工(= 3)像
pID - mID pID-mID
2 - 1 2-1
2 - 2 2-2
2 - 3, 2-3
or, 要么,
should I use a table like 我应该使用像
pID - method pID-方法
2 - forged 2-伪造的
2 - welded 2-焊接
2 - machined 2-机加工

What is performance-wise (lots of products and lots of methods) the smartest thing to do? 最明智的做法是在性能方面(很多产品和许多方法)? Perform a bunch of query every time the application loads a different product (which happens a lot), or one query from the products table and load the methods this way (splitting the string and comparing each method with the checkboxes in a for loop)? 每次应用程序加载不同的产品(经常发生)时执行一堆查询,或者从products表中执行一个查询并以这种方式加载方法(拆分字符串并将每个方法与for循环中的复选框进行比较)?

You should design the database as normalized. 您应该将数据库设计为规范化的。 It it not necessarily the fastest option, but it is definitely easiest to manage. 它不一定是最快的选择,但绝对是最容易管理的。 Most probably You want table of production methods anyway, so many-to-many relationship is a natural choice. 无论如何,您很可能仍希望使用生产方法表,因此多对多关系是自然的选择。 It also makes tasks like filtering by production method a piece of cake. 它还使诸如通过生产方法进行过滤之类的任务变得轻而易举。 You really do not want to parse string on server to achieve this. 您真的不想解析服务器上的字符串来实现此目的。

You can always precalculate those concatenated strings into table if You need faster response. 如果您需要更快的响应,则始终可以将这些串联的字符串预先计算到表中。 Just do not use them as primary data. 只是不要将它们用作主要数据。

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

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