问题描述
有趣的事情我发现了mysql.MySQL 有一个 3 字节的数字类型 - MEDIUMINT.它的范围是从 -8388608 到 8388607.对我来说似乎很奇怪.为了更好的性能选择的数字类型的大小,我认为数据应该与机器字或双字对齐.如果我们需要一些数字范围的限制规则,它必须是相对于数据类型的外部.例如:
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
)
那么,有谁知道为什么是 3 个字节?有什么原因吗?
So, does anyone know why 3 bytes? Is there any reason?
推荐答案
您提到的对齐问题主要适用于 RAM 中的数据.没有什么会强制 MySQL 在处理它时使用 3 个字节来存储该类型.
The alignment issue you mentioned applies mostly to data in RAM. Nothing forces MySQL to use 3 bytes to store that type as it processes it.
不过,这在更有效地使用磁盘缓存方面可能有一个小优势.
This might have a small advantage in using disk cache more efficiently though.
这篇关于mysql 中 3 字节数字的含义 (MEDIUMINT)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!