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

后端 未结 4 1189
面向向阳花
面向向阳花 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条回答
  •  猫巷女王i
    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.

提交回复
热议问题