简体   繁体   English

简单的MySQL表设计

[英]Simple MySQL table design

I think I should be counted as database newbie, so read the question as a newbie question. 我想我应该算作数据库新手,所以把这个问题看成是一个新手问题。 I currently create a table, which holds environment variables for a number of hosts, like this: 我目前创建了一个表,它包含许多主机的环境变量,如下所示:

create table envs ( 
  host varchar(255), 
  envname varchar(255), 
  envvalue varchar(8192), 
  PRIMARY KEY(host, envname)
);

Very simple, one table holding all the data I need. 非常简单,一张表包含我需要的所有数据。 Common operation is to get all the environment variables for a given host, another is to get a given environment variable for a given host, third example operation would be to get a given environment variable for all hosts and list duplicates. 常见操作是获取给定主机的所有环境变量,另一个是获取给定主机的给定环境变量,第三个示例操作是获取所有主机的给定环境变量并列出重复项。

Performance is not expected to be an issue, it's going to be maybe tens of hosts, dozens of variables per host, average max 1 query per second. 性能预计不会成为问题,它可能是数十台主机,每台主机数十个变量,平均每秒最多1次查询。

Now I've read that having composite primary key is not necessarily a good idea. 现在我已经读过,拥有复合主键并不一定是个好主意。 Is this true for above use case? 对于上述用例,这是真的吗? If it is true, how should I change the database design? 如果是这样,我该如何更改数据库设计? If not, is the above one-table database fine for the purposes I listed above? 如果没有,上面的单表数据库是否适用于我上面列出的目的?

I don't see a problem here with the primary key. 我没有看到主键的问题。 The semantics of a primary key is to uniquely identify the non-key attribute values for the key values. 主键的语义是唯一地标识键值的非键属性值。 As I assume that for one host and one envname there is at most one envvalue the primary key makes perfect sense. 我假设对于一个主机和一个envname,最多只有一个envvalue,主键很有意义。

It could be that some people argue against composite primary keys because they are afraid of performance issues. 可能有些人反对复合主键,因为他们害怕性能问题。 However performance considerations should never influence the choice of the primary key. 但是,性能考虑永远不应影响主键的选择。 Many database systems automatically create an index structure for the primary key; 许多数据库系统自动为主键创建索引结构; the choice of this index structure can influence performance. 这种指数结构的选择会影响绩效。 However this choice can mostly be changed manually and should be done at a later point if you really have performance issues. 但是,这种选择大多可以手动更改,如果确实存在性能问题,应该在以后完成。

Your one-table design and choice of primary key is fine. 您的单桌设计和主键选择都很好。

Now I've read that having composite primary key is not necessarily a good idea. 现在我已经读过,拥有复合主键并不一定是个好主意。 Is this true for above use case? 对于上述用例,这是真的吗?

No. Use a composite primary key on (host, envname) . 否。在(host, envname)上使用复合主键。

If it is true, how should I change the database design? 如果是这样,我该如何更改数据库设计?

N/A. N / A。

If not, is the above one-table database fine for the purposes I listed above? 如果没有,上面的单表数据库是否适用于我上面列出的目的?

Yes: it's known as the Entity–Attribute–Value model . 是的:它被称为实体 - 属性 - 值模型

It's a bad idea, because you store unique values (host, envname) several times. 这是一个糟糕的主意,因为你存储唯一值(主机,ENVNAME) 几次

What if you were to change the hostname from srv01 to *srv01_new*? 如果您要将主机名从srv01更改为* srv01_new *怎么办? You'd have to change every ocurrence of srv01 in your table. 您必须更改表中srv01的 每个发生次数 And what if, some day, you decide you need to create a new table that holds additional information about every single host. 如果有一天你决定需要创建一个包含每个主机的附加信息的新表,那该怎么办呢?

Now, if you change the hostname, you have to change those information as well. 现在,如果更改主机名,则还必须更改这些信息。

To get to your question: It's not an issue of performance , but of normalization . 提出你的问题:这不是性能问题,而是规范化问题

Databases should generally be normalized as far as possible. 数据库通常应尽可能标准化。 If you are intrigued enough, read on . 如果您足够感兴趣, 请继续阅读

You should create one table for your hosts, having a unique id (int) as primary key and a unique (index) name as the hostname. 您应该为主机创建一个表,其中唯一的id (int)作为主键,唯一(索引) 名称作为主机名。

Your table should then only reference the id of the host, not the name . 然后,您的表应仅引用主机的ID ,而不是名称 This way, your hostname is only stored once in your whole database and can be altered to whatever you desire, without breaking other tables. 这样,您的主机名只会在整个数据库中存储一次 ,并且可以更改为您想要的任何内容,而不会破坏其他表。


If your environment names are unique, too, you should create another table for those, having the same layout as the hosts table (id, name). 如果您的环境名称也是唯一的,则应为这些创建另一个表,其布局与hosts表(id,name)相同。

Your combination table then stores the id of the host and the id of the environment , along with the value. 然后,组合表将存储主机ID和环境ID以及值。 You must of course keep the combined primary key, so every combination of host/environment is unique and easily indexable. 您当然必须保留组合的主键,因此主机/环境的每个组合都是唯一的且易于索引。

Then, you have a many-to-many-relationship with additional attributes and perfect normalization. 然后,您与其他属性和完美规范化具有多对多关系。

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

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