简体   繁体   English

将新表用于性别或将其添加为人员表中的字段

[英]Use new table for gender or add it as field in person table

So basically which is better in terms of performance and space: 所以基本上哪个在性能和空间方面更好:

idPerson  Name  Gender   Age
1         John  Male    19
2         Anna  Female  22

or this way: 或者这样:

idPerson  Name  Age  idGender
1         John  19   1
2         Anna  22   2

idGender  Gender
1        Male
2        Female

I think the second one is better because by repeating Male/Female each time will consume more space but I think it may be slower cause of relation. 我认为第二个更好,因为通过重复男性/女性每次会消耗更多的空间,但我认为它可能是较慢的关系原因。 I am wrong? 我错了? There is better way? 还有更好的方法吗?

which is better in terms of performance and space 这在性能和空间方面更好

Most decisions in Computer Science are usually a trade-off between time and space. 计算机科学中的大多数决策通常都是时间和空间之间的权衡。

In your case, the first solution (storing a string up to 6 characters in the table) will ultimately use more space but results in that string being directly available to queries upon the table; 在您的情况下,第一个解决方案(在表中存储最多6个字符的字符串)最终将使用更多空间,但会导致该字符串直接可用于表上的查询; whereas the second solution (storing an integer representation whose meaning can be looked up in a second table) will use less space but requires more work to discover the string (although it does enable one to restrict the possible values by defining a foreign key constraint ). 而第二个解决方案(存储一个整数表示,其含义可以在第二个表中查找)将使用更少的空间,但需要更多的工作来发现字符串(尽管它确实能够通过定义外键约束来限制可能的值) 。

Another approach might be to use MySQL's ENUM type, which enables you to work with strings whilst effectively storing only an integer "under the hood". 另一种方法可能是使用MySQL的ENUM类型,这使您可以使用字符串,同时有效地存储“引擎盖下”的整数。 You may wish to consider Chris Komlenic's article 8 Reasons Why MySQL's ENUM Data Type Is Evil before deciding upon this course of action. 在决定采取这种行动之前,您可能希望考虑Chris Komlenic的第8条原因,即MySQL的ENUM数据类型是邪恶的。

Assuming gender as a binary field, I would go with a third option: name the column MALE and the valid values will be either Y or N (or 1 / 0 ) 假设性别作为二进制字段,我会去的第三个选项:命名列MALE和有效值将或者YN (或1 / 0

Things to consider: other genders like transgender ;) 需要考虑的事项:其他性别如变性人;)

The best solution might be a combination of the both where you use a single character column inside the Person table to represent a Gender code that you map to in your application. 最佳解决方案可能是两者的组合,您在Person表中使用单个字符列来表示您在应用程序中映射到的性别代码。

You can use ' M ' and ' F ' as codes for Male and Female, but now you're free to map any additional codes to whatever exotic genders you might think of. 您可以使用“ M ”和“ F ”作为男性和女性的代码,但现在您可以将任何其他代码映射到您可能想到的任何外国性别。

I will go with first approach as it doesn't make much sense to split and keep gender information separate because its not going to change :). 我将采用第一种方法,因为分开并保持性别信息分离没有多大意义,因为它不会改变:)。 Had it been a scenario where gender name could change then i would have taken second approach. 如果性别名称可能发生变化,那么我会采取第二种方法。

EDIT: Adding this in response to below comment. 编辑:添加此以响应以下评论。 A single table will be faster to query as you dont need any joins, so better performance. 单个表查询速度更快,因为您不需要任何连接,因此性能更佳。 I dont think either approach will make much differnce in terms of space. 我不认为这两种方法在空间方面会有很大不同。

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

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