简体   繁体   English

父子表MySQL

[英]Parent and child tables MySQL

I was trying to figure out the difference (if any) between two mutually exclusive ways to design a DB. 我试图找出设计数据库的两种互斥方式之间的区别(如果有)。

Suppose we have a table of users consisting of userID 's, what would be considered 'best practice' in terms of storing user data? 假设我们有一个由userID组成的userID表,就存储用户数据而言,什么被视为“最佳实践”?

Our Table: 我们的桌子:

userID PK. 

I've seen both of the following being used and it feels that the last one is less useful: 我已经看到了以下两种用法,并且觉得最后一个用处不大:

meta data stored: 元数据存储:
Method 1: 方法1:

meta_id(pk), userID(fk), attribute1, attribute 2 ... attribute n  

Method 2: 方法2:

meta_id(pk), userID(fk),attribute_name,attribute_value  

So instead of having one single meta row for each user, we end up with multiple ones. 因此,我们没有为每个用户创建一个单独的元行,而最终得到多个。 This is the design WordPress uses. 这是WordPress使用的设计。

My Questions: 我的问题:

  1. Is one method slower than the other performing queries? 一种方法比另一种执行查询的速度慢吗?
  2. Are both of them considered industry standards, and if so, what are the use cases for each method? 它们是否都被视为行业标准,如果是,则每种方法的用例是什么?

Well both of the question does not have a sharp answer. 那么,这两个问题都没有一个敏锐的答案。 Both are different techniques, both are different designs, both needs different query styles. 两者都是不同的技术,都是不同的设计,都需要不同的查询样式。

For use case, i think pretty simple for this question. 对于用例,我认为这个问题非常简单。

  • If your attribute types and number of attrs. 如果您的属性类型和属性数量。 are constant (or close to constant) method1 should be used. 如果是常数(或接近常数),则应使用method1。 More specifically, If you can set a logical limit to the number of attrs. 更具体地说,如果可以将逻辑限制设置为attrs的数量。 then you have an "n" in method1. 那么您在method1中有一个“ n”。 (since field names are not flex. attr names should be same between objects that you store just like html) (因为字段名称不是flex。在存储的对象之间,attr名称应该相同,就像html一样)

  • If you have a random number of attr. 如果您有随机数的attr。 & random number of attr. &attr的随机数。 names. 名。 Then method 2 should be used. 然后应使用方法2。 Cause it can adjust to any permutation of (#ATRname , $ATRvalue and #ATRS). 因为它可以调整为(#ATRname,$ ATRvalue和#ATRS)的任何排列。

If the correct design used for a DB. 如果用于DB的设计正确。 performance will not be a problem. 性能不会有问题。

Some more disgusting examples 一些令人作呕的例子

  1. XML objects stored in db. XML对象存储在db中。

    • metod1 metod1
      • all xml objects can not have more then n number of attributes. 所有xml对象不能具有n个以上的属性。
      • unused attrs. 未使用的属性。 means unnecessary storage space and slow quering. 意味着不必要的存储空间和缓慢的查询。
      • inner value will require another fk which points another row in the same table.not a professional method. 内部值将需要另一个fk来指向同一表中的另一行。不是专业方法。
      • If this table is small, queries will work much faster. 如果此表很小,查询将更快地工作。 However when it comes to industrial usage. 但是,当涉及到工业用途时。 This table literally can not return any values because of its size. 由于其大小,该表实际上不能返回任何值。
      • Simple 100kb xml file can need thousands of rows. 简单的100kb xml文件可能需要数千行。
      • very flexible. 非常灵活。 one table can store one/more files or/and one/more objects in one table. 一个表可以在一个表中存储一个/多个文件或/和一个/多个对象。
    • metod2 metod2
      • xml objects can have any number of attributes and attribute names. xml对象可以具有任意数量的属性和属性名称。
      • unused attrs. 未使用的属性。 will not enlarge DB. 不会扩大数据库。
      • all tables and rows can be showed in file. 所有表和行都可以在文件中显示。 Pro stuff. 专业的东西。
      • If this db is small, queries will slower because of the number of relations. 如果此db小,则由于关联数,查询将变慢。 However when it comes to industrial usage. 但是,当涉及到工业用途时。 DB is possible to use DB可以使用
      • Simple 100kb xml file can need tens of rows * tens of tables. 一个简单的100kb xml文件可能需要数十行*数十个表。
      • all tables need to have a specific purpose about what to store. 所有表都需要具有关于存储内容的特定目的。
  2. HTML1.0 objects stored in db. HTML1.0对象存储在db中。

    • metod1 metod1
      • n is the #attributes that is suppoerted by html1.0 . n是html1.0支持的#attributes。
      • unused attrs. 未使用的属性。 means unnecessary storage space and slow quering because of row size&number (not that much compared to other). 意味着不必要的存储空间和由于行大小和行数而导致的查询缓慢(与其他行相比没有那么多)。
      • If this table is small, queries will work much faster. 如果此表很小,查询将更快地工作。 However when it comes to industrial usage, thanks to html hierarchy tables/files can be divided easily. 但是,当涉及到工业用途时,由于使用了html层次结构表/文件,因此可以轻松进行划分。
      • number of html = number of rows. html的数量=行数。 Delicious 美味的
      • not flexible however html hierarchy is not flex. 不灵活,但是html层次结构不灵活。 too. 太。 one table can store one/more files or/and one/more objects in one table.Over usage of tables and rel.s 一个表可以在一个表中存储一个/多个文件或/和一个/多个对象。
    • metod2 metod2
      • html objects can have any number of attributes and attribute names. html对象可以具有任意数量的属性和属性名称。 But there is specific number of attrs. 但是有特定数量的属性。 that html obj. 该html obj。 can have. 可以有。 = Lots of unnecessary tables relations etc. =许多不必要的表关系等
      • unused attrs. 未使用的属性。 will not enlarge DB. 不会扩大数据库。
      • all tables and rows can be showed in file. 所有表和行都可以在文件中显示。 = no null cells =没有空单元格
      • If this db is small, queries will slower because of the number of relations. 如果此db小,则由于关联数,查询将变慢。 However when it comes to industrial usage (assuming the complexity& randomness of an html obj and an xml obj, as it is ;) ) table 但是,当涉及到工业用途时(假定html obj和xml obj的复杂性和随机性;))
      • Simple 100kb html file can need hundreds of tables. 一个简单的100kb html文件可能需要数百张表。

Yes both are former designs for industrial DBs. 是的,两者都是工业DB的先前设计。 for different purposes. 用于不同的目的。

I assumed that xml objects has random number of subobject with random attr. 我假设xml对象具有随机属性的子对象随机数。 names . 名字。 Unlike html. 与html不同。 It just have limited number of attrs. 它的attrs数量有限。 in real-life example (because of html is an code representation of visual boxes) it is not be used for complex structures. 在实际示例中(由于html是可视框的代码表示),它不用于复杂的结构。

In addition 此外

  • The most professional design use both of them combined. 最专业的设计将两者结合使用。 For example. 例如。 2.table looks like a MANY TO MANY REL. 2.table看起来像一对多关系。 between TABLE1.1xTABLE1.2...xTABLE1.N. 在TABLE1.1xTABLE1.2 ... xTABLE1.N之间。

  • another thing is. 另一件事是。 i do not feel like meta-id and former-id used together. 我不觉得meta-id和以前的id一起使用。 one is fk and one is pk. 一个是fk,一个是pk。 which is weird because pk means it can be used for relations. 这很奇怪,因为pk意味着它可以用于关系。 fk means, it should be unique (pro stuff not necessarily) === another mutant pk. fk表示,它应该是唯一的(不一定是专业的东西)===另一个突变体pk。 It probably helps to backend operations or updates , merging etc. 它可能有助于后端操作或更新,合并等。

I hope it helped. 希望对您有所帮助。

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

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