views:

41

answers:

3

Currently we're using INT(21)* for all autoincrement id columns in out 30+ table database.

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:

  • If using INT(21) when I am sure we'll never need it is a waste of space
  • If it is a waste, what the recommended size for an autoincrement id column is

*Not my design. I'm asking this because I'm considering reducing this to say, INT(10).

+2  A: 

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.

Conclusion

INT(10) or INT(21), doesn't impact the values that can be stored. If you really have a concern, the data type can easily be changed to be BIGINT with no reprocusions that I'm aware of. 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.

OMG Ponies
Thank you for your detailed answer and the link. My ignorance, in this case, has been eradicated.
Michael Robinson
+1  A: 

See here for the limit of each int type: http://dev.mysql.com/doc/refman/5.0/en/numeric-types.html

Note that 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).

Coronatus
+1  A: 

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.

Galen
If our database runs out of space, we'll have 4294967295 members. Then I'll be kicking myself alright, kicking myself all the way to the bank :)
Michael Robinson