对于该数据,最有效的 MySQL 列类型是什么?

发布于 2024-09-02 22:51:35 字数 452 浏览 4 评论 0原文

我有几个表,每个表都有一些非常标准的数据。有人可以告诉我该数据的最佳列类型来帮助我优化它们吗?他们旁边的是我目前拥有的。

Number (max length 7) --> MEDIUMINT(8) Unsigned
Text (max length 30) --> VARCHAR(30)
Text (max length 200) --> VARCHAR(200)
Email Address (max length 200) --> VARCHAR(200)
Number (max length 4) --> SMALLINT(5) Unsigned
Number (either 0 or 1) --> TINYINT(1) Unsigned
Text (max length 500) --> TEXT

有什么建议吗?我只是猜测,所以我知道其中一些是错误的......

I have several tables with some pretty standard data in each. Can somebody help me optimize them by telling me the best column types for this data. Whats beside them is what I have currently.

Number (max length 7) --> MEDIUMINT(8) Unsigned
Text (max length 30) --> VARCHAR(30)
Text (max length 200) --> VARCHAR(200)
Email Address (max length 200) --> VARCHAR(200)
Number (max length 4) --> SMALLINT(5) Unsigned
Number (either 0 or 1) --> TINYINT(1) Unsigned
Text (max length 500) --> TEXT

Any suggestions? I'm just guessing with this so I know some of them are wrong...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

就此别过 2024-09-09 22:51:35

抱歉,这不是您问题的直接答案,但我认为这需要指出。我认为您可能误解了列类型后面括号中的整数的用途。

对于 VARCHAR 类型,您可能已经知道,它限制最大长度。但是,它不会影响用于存储特定字符串的字节数。长度为 5 的字符串无论存储在 VARCHAR(100) 还是 VARCHAR(200) 中都需要相同数量的字节存储。

对于整数类型,该数字与存储的字节数完全无关。它是显示宽度,这是别的东西。请参阅手册

MySQL 支持另一个扩展,可以选择在类型的基本关键字后面的括号中指定整数数据类型的显示宽度(例如,INT(4))。应用程序可以使用此可选显示宽度来通过用空格向左填充宽度来显示宽度小于为列指定的宽度的整数值。 (也就是说,这个宽度存在于随结果集返回的元数据中。是否使用它取决于应用程序。)

显示宽度不限制列中可以存储的值的范围,也不限制宽度超过为列指定的值的显示位数。

Sorry that this isn't a direct answer to your question but I think this needs pointing out. I think you may have misunderstood the purpose of the integer in brackets after the column type.

For VARCHAR types, as you probably already know, it restricts the maximum length. However it doesn't affect the number of bytes used for storage of a specific string. A string of length 5 will require the same number of bytes storage whether it is stored in a VARCHAR(100) or a VARCHAR(200).

For integral types the number has nothing at all to do with the number of bytes of storage. It is the display width, which is something else. See the manual:

Another extension is supported by MySQL for optionally specifying the display width of integer data types in parentheses following the base keyword for the type (for example, INT(4)). This optional display width may be used by applications to display integer values having a width less than the width specified for the column by left-padding them with spaces. (That is, this width is present in the metadata returned with result sets. Whether it is used or not is up to the application.)

The display width does not constrain the range of values that can be stored in the column, nor the number of digits that are displayed for values having a width exceeding that specified for the column.

笑叹一世浮沉 2024-09-09 22:51:35
Number (either 0 or 1) --> TINYINT(1) Unsigned

那应该是一个布尔值。

Number (either 0 or 1) --> TINYINT(1) Unsigned

That should be a Boolean.

烟凡古楼 2024-09-09 22:51:35

你已经说得很明智了。

请注意,您无法对列类型进行任何优化。使用索引即可。

You've got it pretty sensible already.

Please note, you can't optimize anything with column types. With indexes you do.

梦过后 2024-09-09 22:51:35

取决于你对“高效”的定义。就速度而言,CHAR 可能比 VARCHAR 更快(因为每行最终的长度相同,使得查找给定记录变得简单)。但所有字段都必须有固定长度,否则就不用担心。

Depends on your definition of "efficient". For speed, CHAR can be faster than VARCHAR (since each row ends up the same length, making it simple to seek to a given record). But all of your fields have to have fixed lengths, or don't bother.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文