SQL Server 数据类型:将 8 位无符号数存储为 INT 还是 CHAR(8)?

发布于 2024-09-30 05:50:20 字数 216 浏览 3 评论 0原文

我认为标题说明了一切。 将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

猫弦 2024-10-07 05:50:20

鉴于 TAC 可以有前导零,它们实际上是一个不透明的标识符,并且永远不会用来计算,请使用 char 列。

在确定已正确建模数据类型之前,不要开始优化空间。

编辑

但是为了避免其中出现垃圾,请确保还应用了 CHECK 约束。例如,如果它是 8 位数字,请添加

CONSTRAINT CK_Only8Digits CHECK (not TAC like '%[^0-9]%' and LEN(RTRIM(TAC)) = 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

CONSTRAINT CK_Only8Digits CHECK (not TAC like '%[^0-9]%' and LEN(RTRIM(TAC)) = 8)
韬韬不绝 2024-10-07 05:50:20

如果是数字,则将其存储为数字。

整数使用 4 字节 存储,给出范围:

-2^31 (-2,147,483,648) 到 2^31-1 (2,147,483,647)

所以,适合您的需求。

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:

-2^31 (-2,147,483,648) to 2^31-1 (2,147,483,647)

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 from char[8] to char[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 the char[8] data to it (to keep things reliable and consistent), then remove the char[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).

无戏配角 2024-10-07 05:50:20

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.

独享拥抱 2024-10-07 05:50:20

对于这个,请坚持使用 INT,DEFFINITELY INT(或 BIGINT)

看一下 int、bigint、smallint 和tinyint (Transact-SQL)

-2^31 的整数(整数)数据
(-2,147,483,648) 到 2^31 - 1
(2,147,483,647)。存储大小为4
字节,

来自-2^63的bigint(整数)数据
(-9,223,372,036,854,775,808) 至
2^63-1 (9,223,372,036,854,775,807)。
存储大小为8字节。

相比

char 和 varchar

固定长度非 Unicode 字符
长度为n个字节的数据。 n 必须是
1 到 8,000 之间的值。贮存
大小为 n 字节。

另外,一旦你对此进行查询,如果你使用 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)

Integer (whole number) data from -2^31
(-2,147,483,648) through 2^31 - 1
(2,147,483,647). Storage size is 4
bytes,

bigint (whole number) data from -2^63
(-9,223,372,036,854,775,808) through
2^63-1 (9,223,372,036,854,775,807).
Storage size is 8 bytes.

compared to

char and varchar

Fixed-length non-Unicode character
data with length of n bytes. n must be
a value from 1 through 8,000. Storage
size is n bytes.

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文