MySQL - "Lookup" 列数据类型 - 存储简单查找的理想方式
MySQL - "Lookup" column data type - ideal way to store simple lookups
我在工作中听一些人谈论数据库专栏。
本质上,我们想添加一个新列作为查找的 FK table。它基本上是您首选的联系方式(主要 phone 或主要电子邮件)。所以 main table 'User' 有一个 FK 到 'PreferredContactMethod'
第 1 个人:
"Let's store the FK column as an unsigned tiny int, and make the PK lookup table simply have a tinyint PK and a text description/code"
第 2 个人
"It's irreleant whether we store the datatype as unsigned tinyint, or char(x) in terms of space in MySQL, so why make you have to do joins to find out what the value is for the lookup column? Instead, make the FK a char(x), and make the PK char(x) on the actual table"
第 3 个人
"No it's not a good idea to make a PK represented as characters. It's not efficient. Handling something like unsigned tinyint is better than text. And since there are only two values, why don't we just store it as a single column (not an FK) with a value of either 0, or 1. This way it's more efficient and you don't have to join anything."
所以听了这一切,我开始怀疑谁是对的。我怀疑这太微不足道了,在性能方面无关紧要,但我现在很好奇我 喜欢 某人的优点和缺点是什么关于这个。
感谢您的宝贵时间。
像这样的问题通常没有正确或错误的答案。或者实际上,任何一个答案都可能是正确的,因为这取决于您将如何使用数据。
将 int/tinyint 存储到查找 table 的一个很好的例子是值定期更改,并且您希望允许在查找 table 中发生更改而不更改所有引用它的行。
如果您有一个相对较小的查找 table 并且不经常更改,并且字符串相当短,那么将 PK 存储为字符串是一件好事。如果字符串很长,这可能会使 FK 参考变得更庞大,并且需要使用大量 space.
将 PK 存储为字符串的低效率对查找影响不大 table。 table 很小。字符串 PK 的成本主要是由于频繁的随机插入。但这不会影响查找 table.
我在工作中听一些人谈论数据库专栏。 本质上,我们想添加一个新列作为查找的 FK table。它基本上是您首选的联系方式(主要 phone 或主要电子邮件)。所以 main table 'User' 有一个 FK 到 'PreferredContactMethod'
第 1 个人: "Let's store the FK column as an unsigned tiny int, and make the PK lookup table simply have a tinyint PK and a text description/code"
第 2 个人 "It's irreleant whether we store the datatype as unsigned tinyint, or char(x) in terms of space in MySQL, so why make you have to do joins to find out what the value is for the lookup column? Instead, make the FK a char(x), and make the PK char(x) on the actual table"
第 3 个人 "No it's not a good idea to make a PK represented as characters. It's not efficient. Handling something like unsigned tinyint is better than text. And since there are only two values, why don't we just store it as a single column (not an FK) with a value of either 0, or 1. This way it's more efficient and you don't have to join anything."
所以听了这一切,我开始怀疑谁是对的。我怀疑这太微不足道了,在性能方面无关紧要,但我现在很好奇我 喜欢 某人的优点和缺点是什么关于这个。
感谢您的宝贵时间。
像这样的问题通常没有正确或错误的答案。或者实际上,任何一个答案都可能是正确的,因为这取决于您将如何使用数据。
将 int/tinyint 存储到查找 table 的一个很好的例子是值定期更改,并且您希望允许在查找 table 中发生更改而不更改所有引用它的行。
如果您有一个相对较小的查找 table 并且不经常更改,并且字符串相当短,那么将 PK 存储为字符串是一件好事。如果字符串很长,这可能会使 FK 参考变得更庞大,并且需要使用大量 space.
将 PK 存储为字符串的低效率对查找影响不大 table。 table 很小。字符串 PK 的成本主要是由于频繁的随机插入。但这不会影响查找 table.