Mysql – Should I use TINYINT for ID (PK) field in MySQL

MySQLschema

Should I perform this type of optimisation if I know that a table can't have more records than 10?

Best Answer

Yes. You should also say UNSIGNED: TINYINT UNSIGNED. Range 0..255; 1 byte.

Actually, it is such a tiny optimization that I don't bother mentioning it.

Think about using the smallest datatypes when you first CREATE the table; it is messier to make the change later.

SMALLINT UNSIGNED: 0..65535, 2 bytes
MEDIUMINT UNSIGNED: 0..16M, 3 bytes
INT UNSIGNED: 0..4B, 4 bytes

Keep in mind that certain operations "burn" AUTO_INCREMENT ids. This could lead to an unexpected overflow. Examples: INSERT IGNORE and REPLACE. They may preallocate an id before realizing that it won't be needed.