Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of 3 byte numeric in mysql (MEDIUMINT)

Tags:

types

mysql

Funny thing I've found abount mysql. MySQL has a 3 byte numeric type - MEDIUMINT. Its range is from -8388608 to 8388607. It seems strange to me. Size of numeric types choosen for better performance, I thought data should be aligned to a machine word or double word. And if we need some restriction rules for numeric ranges, it must be external relative to datatype. For example:

CREATE TABLE ... (
  id INT RANGE(0, 500) PRIMARY KEY
)

So, does anyone know why 3 bytes? Is there any reason?

like image 501
Denis Bazhenov Avatar asked Aug 31 '25 16:08

Denis Bazhenov


1 Answers

The reason is so that if you have a number that falls within a 3 byte range, you don't waste space by storing it using 4 bytes.

When you have twenty billion rows, it matters.

like image 197
chaos Avatar answered Sep 02 '25 07:09

chaos