SQL Server: varbinary or int to store a bit mask?

后端 未结 4 1179
面向向阳花
面向向阳花 2021-02-20 06:45

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

相关标签:
4条回答
  • 2021-02-20 07:24

    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.

    0 讨论(0)
  • 2021-02-20 07:32

    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.

    0 讨论(0)
  • 2021-02-20 07:47

    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.

    0 讨论(0)
  • 2021-02-20 07:48

    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.

    0 讨论(0)
提交回复
热议问题