SQL Server 数据类型:将 8 位无符号数存储为 INT 还是 CHAR(8)?
我认为标题说明了一切。 将 8 位无符号数存储为 Int 类型还是 char(8) 类型更好(更快,根据内存和磁盘节省空间)? 当我使用固定字符长度时,如果将来数字变为 9 位,我会遇到麻烦吗?
背景信息:我想存储 TAC
谢谢
i think the title says everything.
Is it better(faster,space-saving according memory and disk) to store 8-digit unsigned numbers as Int or as char(8) type?
Would i get into trouble when the number will change to 9 digits in future when i use a fixed char-length?
Background-Info: i want to store TACs
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
鉴于 TAC 可以有前导零,它们实际上是一个不透明的标识符,并且永远不会用来计算,请使用 char 列。
在确定已正确建模数据类型之前,不要开始优化空间。
编辑
但是为了避免其中出现垃圾,请确保还应用了 CHECK 约束。例如,如果它是 8 位数字,请添加
Given that TACs can have leading zeroes, that they're effectively an opaque identifier, and are never calculated with, use a char column.
Don't start optimizing for space before you're sure you've modelled your data types correctly.
Edit
But to avoid getting junk in there, make sure you apply a CHECK constraint also. E.g if it's meant to be 8 digits, add
如果是数字,则将其存储为数字。
整数使用 4 字节 存储,给出范围:
所以,适合您的需求。
char[8]
将存储为 8 个字节,因此存储量增加了一倍,当然还会受到将来扩展的需要(将近 10M 记录从 8 个字符转换为 9 个字符需要时间,并且可能需要在此期间使数据库脱机)。因此,从存储、速度、内存和磁盘使用(所有这些都与数据类型使用的字节数有关)、可读性、语义和面向未来来看,
int
毫无疑问胜出。更新
既然您已经澄清您不存储数字,我想说您必须使用
char
来保留前导零。至于未来扩展的问题 - 由于
char
是固定长度字段,因此从char[8]
更改为char[9]
不会丢失信息。但是,我不确定附加字符是否会添加在右侧或左侧(尽管这可能尚未确定)。您必须进行测试,一旦字段扩展,您将需要确保原始数据已被保留。更好的方法可能是创建一个新的 char[9] 字段,将所有 char[8] 数据迁移到其中(以保持可靠和一致),然后删除
char[8]
字段并将新字段重命名为原始名称。当然,这会破坏表中的所有统计数据(但直接扩展字段也会破坏)。If it is a number, store it as a number.
Integers are stored using 4 bytes, giving them the range:
So, suitable for your needs.
char[8]
will be stored as 8 bytes, so double the storage, and of course suffers from the need to expand in the future (converting almost 10M records from 8 to 9 chars will take time and will probably require taking the database offline for the duration).So, from storage, speed, memory and disk usage (all related to the number of bytes used for the datatype), readability, semantics and future proofing,
int
wins hands down on all.Update
Now that you have clarified that you are not storing numbers, I would say that you will have to use
char
in order to preserve the leading zeroes.As for the issue with future expansion - since
char
is a fixed length field, changing fromchar[8]
tochar[9]
would not lose information. However, I am not sure if the additional character will be added on the right or left (though this is possibly undetermined). You will have to test and once the field has been expanded you will need to ensure that the original data has been preserved.A better way may be to create a new
char[9]
field, migrate all thechar[8]
data to it (to keep things reliable and consistent), then remove thechar[8]
field and rename the new field to the original name. Of course this would ruin all statistics on the table (but so would expanding the field directly).int 比 char 使用更少的内存空间并提供更快的索引。
如果您需要将这些数字分开 - 搜索数字 3-4 为“02”或类似数字的所有内容 - char 会更简单并且可能更快。
我猜你没有对它们进行算术运算。您不会将两个 TAC 添加在一起或查找一组记录的平均 TAC 或类似的内容。如果是的话,那将是使用 int 的有力论据。
如果它们有前导零,那么使用 char 可能更容易,因此您不必总是用零将数字填充到正确的长度。
如果以上都不适用,那也没有多大关系。我可能会使用 char。我想不出有什么令人信服的理由去选择任何一种方式。
An int will use less memory space and give faster indexing than a char.
If you need to take these numbers apart -- search for everything where digits 3-4 are "02" or some such -- char would be simpler and probably faster.
I gather you're not doing arithmetic on them. You'd not adding two TACs together or finding the average TAC for a set of records or anything like that. If you were, that would be a slam-dunk argument for using int.
If they have leading zeros, its probably easier to use char so you don't have to always pad the number with zeros to the correct length.
If none of the above applies, it doesn't matter much. I'd probably use char. I can't think of a compelling reason to go either way.
对于这个,请坚持使用 INT,DEFFINITELY INT(或 BIGINT)
看一下 int、bigint、smallint 和tinyint (Transact-SQL)
相比
与char 和 varchar
另外,一旦你对此进行查询,如果你使用 int 与你的 char 列相比,你的性能将会下降,因为 SQL Server 必须为你做 as 强制转换...
Stick to INT for this one, DEFFINITELY INT (OR BIGINT)
Have a look at int, bigint, smallint, and tinyint (Transact-SQL)
compared to
char and varchar
Also, once you query against this, you will have degraded performance if you use ints compared to your char column, as SQL Server will have to do as cast for you...