SQL Server:varbinary或int存储位掩码? [英] SQL Server: varbinary or int to store a bit mask?

查看:278
本文介绍了SQL Server:varbinary或int存储位掩码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在性能或灵活性方面,使用int vs 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).

推荐答案

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

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.

如果您需要更多标志,并且必须使用 BINARY ,您将失去对按位运算符的本机支持因此容易支持检查标志值。我可能会向客户端应用程序移动检查标志值,但如果您在T-SQL中编程也是一个选项。如果您使用C#,您可以使用 BitArray 具有必要操作的类,在Java中有一个 BitSet 课程。

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.

这篇关于SQL Server:varbinary或int存储位掩码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆