存储许多位——我应该使用多个列还是单个位字段列?

发布于 2024-09-12 08:28:46 字数 183 浏览 3 评论 0原文

我正在数据库中设计一个User 表。我为每个用户提供了大约 30 个左右的选项,可以是“允许”或“不允许”。

我的问题是,我应该将这些存储为 30 位列,还是应该使用单个 int 列来存储它们并解析应用程序中的每一位?

另外,我们的数据库是SQL Server 2008和2005(取决于环境)

I am designing a User table in my database. I have about 30 or so options for each user that can be either "allow" or "disallow".

My question is should I store these as 30 bit columns or should I use a single int column to store them and parse out each bit in my application?

Also, our database is SQL Server 2008 and 2005 (depending on environment)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

陈甜 2024-09-19 08:28:46

我只是尝试创建两个表,一个具有单个 int 列,一个具有 30 位列,然后向每个表添加一行并使用 SQL Server 内部查看器

CREATE TABLE T_INT(X INT DEFAULT 1073741823);

CREATE TABLE T_BIT(
X1  BIT DEFAULT 1,
/*Other columns omitted for brevity*/
X30 BIT DEFAULT 1
);

INSERT INTO T_INT DEFAULT VALUES;

INSERT INTO T_BIT DEFAULT VALUES;

具有 30 位列的表的单行

BITS

具有一个 int 列的表格的单行

INT

从存储的角度来看,SQL Server 合并了位列,并且数据存储在完全相同的空间量中(黄色)。对于 NULL 位图(紫色),您最终会每行丢失 3 个字节,因为它的长度与列数成正比(无论它们是否允许空值)

字段的键(对于 int 版本,颜色编码与位版本相同)

Int key

I just tried creating two tables, one with a single int column and one with 30 bit columns then added a row to each and looked at them with SQL Server Internals Viewer

CREATE TABLE T_INT(X INT DEFAULT 1073741823);

CREATE TABLE T_BIT(
X1  BIT DEFAULT 1,
/*Other columns omitted for brevity*/
X30 BIT DEFAULT 1
);

INSERT INTO T_INT DEFAULT VALUES;

INSERT INTO T_BIT DEFAULT VALUES;

Single row for table with 30 Bit Columns

BITS

Single row for table with one int Column

INT

From a storage point of view SQL Server combines the bit columns and the data is stored in exactly the same amount of space (yellow). You do end up losing 3 bytes a row for the NULL bitmap (purple) though as the length of this is directly proportional to the number of columns (irrespective of whether they allow nulls)

Key for fields (for the int version, colour coding is the same for the bit version)

Int key

瑕疵 2024-09-19 08:28:46

两者都不是 - 除非您有重大空间问题或与其他系统的兼容性要求,否则请考虑这将如何阻止您优化查询并清楚地理解每个位代表的含义。

一个表中可以有一千多个列,也可以有一个用于用户设置的子表。为什么要限制自己在应用程序中需要解析的 30 位?想象一下,如果其中一些设置被弃用或引入了一些新设置,您需要对应用程序进行什么样的更改。

Neither -- unless you have a major space issue or compatibility requirement with some other system, think about how this will prevent you from optimizing your queries and clearly understanding what each bit represents.

You can have more than a thousand columns in a table, or you can have a child table for user settings. Why limit yourself to 30 bits that you need to parse in your app? Imagine what kind of changes you'll need to make to the app if several of these settings are deprecated or a couple of new ones introduced.

只有一腔孤勇 2024-09-19 08:28:46

我认为如果每个值都有列,那么将来的扩展会更容易。如果您将来添加另一个选项(对于大多数这样的应用程序来说可能是这样),那么它可能会影响您的所有其他代码,因为您需要重新解析 int 列以考虑新位。

I think it would be easier to allow for future expansion if you have columns for each value. If you add another option in the future (which is likely for most applications like this), then it may affect all your other code since you would need to reparse your int column to account for the new bits.

留蓝 2024-09-19 08:28:46

如果您组合成位标志字段,那么在查看原始数据时将很难看到设置的内容。我会为每个值使用单独的列,或者将选项存储在它们自己的表中。

If you combine into a bitflag field, it's going to be difficult to see what is set if you're looking at the raw data. I'd go with individual columns for each value, or store the options in their own table.

手长情犹 2024-09-19 08:28:46

我同意你的设计应该正确标准化,三个表用户和用户设置,以及一个桥接表:

User:

Userid int

UserName varchar(X)

UserSetting

Settingid int

SettingName varchar (X)

UserUserSetting:

Userid int

SettingId int

IsSet bit

桥接表 UserUserSettingUserSetting 和 User 表之间会有 FK,并且有一个唯一的表UserUserSetting 中 t UserId、SettingId 的控制约束

I agree your design should be properly normalized, three tables User and User setting, and a bridge table:

User:

Userid int

UserName varchar(X)

UserSetting:

Settingid int

SettingName varchar(X)

UserUserSetting:

Userid int

SettingId int

IsSet bit

There would be FK's between the bridge table UserUserSetting and the UserSetting and User table and a unique contr constraint of t UserId, SettingId in UserUserSetting

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