SQL Server:varbinary 或 int 来存储位掩码?

发布于 2024-07-18 14:58:44 字数 88 浏览 3 评论 0原文

使用 int 与 varbinary 存储位掩码在性能或灵活性方面有什么优势吗?

出于我的目的,我将始终对这些位掩码进行读取(不写入或更新)。

Is there any advantage of using int vs varbinary for storing bit masks in terms of performance or flexibility.

For my purposes, I will always be doing reads on these bit masks (no writes or updates).

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

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

发布评论

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

评论(4

万人眼中万个我 2024-07-25 14:58:44

您绝对应该使用 INT (如果您需要 32 个标志)或 BIGINT (对于 64 个标志)。 如果您需要更多标志,可以使用 BINARY (但您可能还应该问自己为什么应用程序中需要这么多标志)。

此外,如果您使用整数类型,则可以使用标准按位运算符直接执行,无需将字节数组转换为整型。

如果您确实需要更多标志并且必须使用 BINARY,您将失去对按位运算符的本机支持,因此无法轻松支持检查标志值。 我可能会将标志值检查转移到客户端应用程序,但如果您习惯使用 T-SQL 编程,这也是一个选择。 如果您使用的是 C#,则您有一个 BitArray类,具有必要的操作,在 Java 中,您有一个 BitSet 类。

You should definitely use an INT (if you need 32 flags) or BIGINT (for 64 flags). If you need more flags you could use BINARY (but you should probably also ask yourself why you need so many flags in your application).

Besides, if you use an integral type, you can use standard bitwise operators directly without converting a byte array to an integral type.

If you do need more flags and have to use BINARY you lose native support for bitwise operators and therefore easy support for checking flag values. I would probably move checking for flag values to a client application but if you're comfortable programming in T-SQL that's an option as well. If you're using C# you have a BitArray class with the necessary operations and in Java you have a BitSet class.

想念有你 2024-07-25 14:58:44

通常认为使用一堆位列而不是位掩码更可取。 它们将在页面中挤在一起,因此不会占用更多空间。 虽然我似乎也总是使用 int 或 bigint 列来避免输入所有列名称..但是对于智能感知,我可能会使用位列。

It is generally considered preferable to use a bunch of bit columns instead of a bit mask. They will get packed together in the page, so they won't take any more room. Although I too always seem to go with an int or bigint column to avoid all of the column name typing.. but with intellisense I would probably go with the bit columns.

笙痞 2024-07-25 14:58:44

好吧,考虑到 int 的存储空间较小,并且通常更容易使用,我不确定为什么要使用 varbinary。

Well, considering an int has less storage space and is generally a little easier to work with I'm not sure why you'd use a varbinary.

神也荒唐 2024-07-25 14:58:44

我通常同意 @hainstech 使用位字段的答案,因为您可以显式命名每个位字段来指示它应该存储的内容。 然而,我还没有看到一种实用的方法来与位字段进行位掩码比较。 使用 SQL Server 的按位运算符(&、| 等),可以轻松找出是否设置了一系列标志。 使用相等运算符针对大量位字段来完成此操作还有很多工作。

I usually agree with @hainstech's answer of using bit fields, because you can explicitly name each bit field to indicate what it should store. However I haven't seen a practical approach to doing bitmask comparisons with bit fields. With SQL Server's bitwise operators (&, |, etc...) it's easy to find out if a range of flags are set. A lot more work to do that with equality operators against a large number of bit fields.

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