首页 > 代码库 > 个人觉得存成char(12),优于varchar(12)

个人觉得存成char(12),优于varchar(12)

w

延展一点:0----(还是上边的url),varchar(10)则数据库的存储1-11bytes,而不是0-10bytes;varchar(256)则为2-258bytes; 1----如果待入库string长度为0-12(相对较小,或者说推测或历史记录中大概率为11,12),个人觉得存成char(12),优于varchar(12)(最初设计为或修改为);2-对于入库的string长度过滤和未过滤下的db自处理,是另外的话题了;

https://dev.mysql.com/doc/refman/5.7/en/char.html 

         Values in VARCHAR columns are variable-length strings. The length can be specified as a value from 0 to 65,535. The effective maximum length of a VARCHAR is subject to the maximum row size (65,535 bytes, which is shared among all columns) and the character set used. See Section C.10.4, “Limits on Table Column Count and Row Size”.

In contrast to CHAR, VARCHAR values are stored as a 1-byte or 2-byte length prefix plus data. The length prefix indicates the number of bytes in the value. A column uses one length byte if values require no more than 255 bytes, two length bytes if values may require more than 255 bytes.       

 

个人觉得存成char(12),优于varchar(12)