CHAR(5) 与 INT
INFORMIX-SE 7.1:
假设 col5 CHAR(5) 将仅包含从 00001 到 99999 的数字:
- CHAR(5) 列使用 5 个字节的存储空间,那么 INTEGER 使用多少字节?
- INT 列上的 UNIQUE INDEX 的执行速度是否比 CHAR(5) 更快?
- INT 列上的 SELECT MAX() 是否比 col5 CHAR(5) 上的 MAX() 更快?
INFORMIX-SE 7.1:
Assume that col5 CHAR(5) will only contain numbers from 00001 to 99999:
- a CHAR(5) column uses 5 bytes of storage, how many bytes does an INTEGER use?
- Does a UNIQUE INDEX on an INT column perform faster than a CHAR(5)?
- Is SELECT MAX() on an INT column faster than MAX() on the col5 CHAR(5)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
CHAR(5)
将始终使用 5 个字节的空间(自 mySQL 4.1 起,在多字节编码中为 10 个字节),因为每个字符可以存储 255 个不同的字符(在多字节编码中甚至更多)。无符号
SMALLINT
(0..65535) 将占用 2 个字节,无符号MEDIUMINT
(0....1670 万) 需要 3 个字节。底线:对数字数据使用 CHAR 类型总是浪费空间。
至于另外两个问题:
非常非常有可能是的,因为索引不需要是字母数字,并且可以处理更少量的数据
极可能是的,因为要在 CHAR 列上运行数字函数,必须对每条记录执行类型转换。
参考:
CHAR(5)
will always use 5 bytes of space (10 in multi-byte encodings since mySQL 4.1) because every character can store 255 different characters (or even more in multi-byte encodings).An unsigned
SMALLINT
(0..65535) will take 2, an unsignedMEDIUMINT
(0.... 16,7 Million) 3 bytes.Bottom line: Using a CHAR type for numeric data is always a waste of space.
As to the other two questions:
Very, very likely yes, because the index doesn't need to be alpha-numeric and can work with a smaller amount of data
Extremely likely yes, because to run a numeric function on a CHAR column, a type cast has to be performed on every record.
Reference:
INT 使用 4 个字节。 并且可以存储 0 ... 4294967295(无符号),带 INT
顺便说一句,我不会使用 CHAR、VARCHAR 或任何 string-y 类型来存储数字。你在这里只是自找麻烦。例如,如果使用字符串比较,则 9 大于 10。您还会错过很多算术运算。
INT uses 4 bytes. And you can store 0 ... 4294967295 (unsigned) with INT
By the way I wouldn't use
CHAR
,VARCHAR
, or any string-y type to store numbers. You are just asking for trouble here. For example, 9 is greater than 10 if you use string comparison. You'll also miss out on a lot of arithmetic operations.更接近受支持的版本(比 4.10 或 2.10),但仍需要十年或更长时间才能获得支持。
Getting closer to a supported version (than 4.10, or 2.10), but still a decade and more out of support.
INT 为 4 个字节。
INT
更快!有关更多详细信息,请查看此问题的选定答案:
4 bytes for
INT.
INT
is faster!For more detail, check out the selected answer for this question: