简体   繁体   English

什么尺寸的INT我应该用于我的自动增量ids MySQL

[英]What size INT should I use for my autoincrement ids MySQL

Currently we're using INT(21)* for all autoincrement id columns in out 30+ table database. 目前我们正在对30多个表数据库中的所有自动增量id列使用INT(21)*。

We are a blogging site, and have tables storing members, comments, blog posts and the like. 我们是一个博客网站,并有表格存储成员,评论,博客文章等。

I'm quite sure we will never reach the limit of our INT(21) id columns, and would like to know: 我很确定我们永远不会达到我们的INT(21)id列的限制,并且想知道:

  • If using INT(21) when I am sure we'll never need it is a waste of space 如果使用INT(21),我确信我们永远不需要它是浪费空间
  • If it is a waste, what the recommended size for an autoincrement id column is 如果它是浪费,自动增量id列的建议大小是什么

*Not my design. *不是我的设计。 I'm asking this because I'm considering reducing this to say, INT(10). 我问这个是因为我正在考虑减少这个,说INT(10)。

The value within the brackets is the display width . 括号内的值是显示宽度

[It] 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. 显示宽度不会限制可以存储在列中的值的范围,也不会限制宽度超过为列指定的值的值显示的位数。 For example, a column specified as INT(3) has the usual INT range of -2147483648 to 2147483647, and values outside the range permitted by three characters are displayed using more than three characters. 例如,指定为INT(3)的列具有通常的INT范围-2147483648至2147483647,并且超出三个字符所允许的范围之外的值将使用三个以上的字符显示。

Conclusion 结论

INT(10) or INT(21) , doesn't impact the values that can be stored. INT(10)INT(21)不会影响可以存储的值。 If you really have a concern, the data type can easily be changed to be BIGINT with no repercussions that I'm aware of. 如果您真的有疑虑,可以轻松地将数据类型更改为BIGINT,而不会产生任何我所知道的反响。 I'd look at how many new records are being created in a given period (IE a month) & see how long it'll take to max out the INT value based on that history. 我将查看在给定时间段内创建的新记录数量(IE每月一次),并查看根据该历史记录最大化INT值所需的时间。

See here for the limit of each int type: http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html 请参阅此处了解每种int类型的限制: http//dev.mysql.com/doc/refman/5.0/en/numeric-types.html

Note that INT(21) == INT(100000) . 注意INT(21) == INT(100000) The number in brackets is just how many zeros are padded to it if you specify the field should be zero-padded. 括号中的数字是指如果指定字段应为零填充,则填充多少个零。

An unsigned int field can hold up to 4294967295 records (see link above). unsigned int字段最多可容纳4294967295条记录(参见上面的链接)。

Don't worry about taking too much space, space is cheap. 不要担心占用太多空间,空间便宜。 Choose a column type that you know you won't exceed. 选择您不会超过的列类型。 You'll kick yourself when you choose smallint to save space and run into issues when your database won't hold anymore data. 当您选择smallint以节省空间并在数据库不再保存数据时遇到问题时,您将自己踢。

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

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