tags:

views:

88

answers:

3

A colleague is adding a bit mask to all our database tables. In theory this is so we can track certain properties of each row across the entire system. For example...

  • Is the row shipped with the system or added by the client once they've started using the system
  • Has the row been deleted from the table (soft deletes)
  • Is the row a default value within a set of rows

Is this a good idea? Are there other uses where this approach would be beneficial?

My preference is these properties are obviously important, and having a dedicated column for each property is justified to make what is happening clearer to fellow developers.

+6  A: 

Not really, no.

You can only store bits in it, and only so many. So, seems to me like it's asking for a lot of application-level headaches later on keeping track of what each one means and potential abuse later on because "hey they're everywhere". Is every bitmask on every table going to use the same definition for each bit? Will it be different on each table? What happens when you run out of bits? Add another?

There are lots of potential things you could do with it, but it begs the question "why do it that way instead of identifying what we will use those bits for right now and just make them proper columns?" You don't really circumvent the possibility of schema changes this way anyway, so it seems like it's trying to solve a problem that you can't really "solve" and especially not with bitmasks.

Each of the things you mentioned can be (and should be) solved with real columns on the database, and those are far more self-documenting than "bit 5 of the BitMaskOptions field".

Daniel DiPaolo
+6  A: 

A dedicated column is is better, because it's undoubtedly more obvious and less error-prone. SQL Server already stores BIT columns efficiently, so performance is not an issue.

The only argument I could see for a bitmask is not having to change the DB schema every time you add a new flag, but really, if you're adding new flags that often then something is not right.

Evgeny
To amplify this point: the bit-fields **are** changing the schema, just in a way completely invisible to the DBM, and mostly invisible to its users.
msw
No point adding a reply here, given Evgeny has summarized it so well. The only reason I can think of for a bitmask is to save storage space compared to separate flags, but in the case of SQL Server 8 BIT columns are packed into a single byte anyway. You'd just be complicating every single query against these fields for an illusory gain.So an emphatic +1 to Evgeny.
Cowan
+3  A: 

No, it is not even remotely a good idea IMO. Each column should represent a single concept and value. Bit masks have all kinds of performance and maintenance problems. How do new developers understand what each of the bits mean? How do you prevent someone from accidentally mixing the meaning of the order of the bits?

It would be better to have a many-to-many relationship or separate columns rather than a bit mask. You will be able to index on it, enable referential integrity (depending on approach), easily add new items and change the order of the results to fit different reports and so on.

Thomas