mysql bitwise operations with string columns
Asked Answered
K

0

6

I need to store some flags for user records in a MySQL table (I'm using InnoDB):

---------------------------
| UserId    | Mask        |
| -------------------------
| 1         | 00000...001 |
| 2         | 00000...010 |
---------------------------

The number of flags is bigger than 64, so I can't use a BIGINT or BIT type to store the value.

I don't want to use many-to-many association tables, because each user can have more than one profile, each one with its set of flags and it would grow too big very quickly.

So, my question is, is it possible to store these flags in a VARCHAR, BLOB or TEXT type column and still do bitwise operations on them? If yes, how?

For now I just need one operation: given a mask A with X bits set to 1 what users have at least those X bits set to 1.

Thanks!

EDIT

To anyone reading this, I've found a solution (for me, at least). I'm using a VARCHAR for the mask field and when searching for a specific mask I use this query:

select * from my_table where mask like '__1__1'

Every record that has the 3rd and last bit set to on will be returned. The "_" symbol is a SQL placehoder for "any single character" (mySQL only, perhaps?).

In terms of speed is doing fine right now, will have to check later when my user base grows.

Anyway, thanks for your input. Other ideas welcomed, of course.

Koral answered 15/10, 2012 at 1:12 Comment(5)
Without knowing more about what you're trying to accomplish (from a conceptual perspective) it's hard to suggest an appropriate solution. How many non-zero flags does a user typically have in a profile? How many profiles typical per user? What do the flags actually represent? Does a given flag mean the same thing for all users? How many different flags can exist? A many-to-many flag table may actually be the best solution but without more details nobody here can tell you.Werewolf
I understand more or less what you want to do but you do realise this will make your database not compliant with first normal form? The definition of 1NF is that the table has no repeating groups and that all columns are scalar values. This means a column cannot have arrays, linked lists, tables within tables, or record structures, like those you find in other programming languages. This is going to lead to complications when roles start changing. If you really need to have this type of data stored SQL might NOT be the answer.Whinny
@Jim: the flags represent time periods in which the users are available (1) or not (0). Right now we have 238 periods. Tipically, a user will have two profiles: main profile and a temporary profile (example: is unavailable between two dates).Koral
@Namphibian: my goal is speed, I know this may not be the best theoretic way of doing this.As of now, with 800 users I already have almost 200 thousand records on the many-to-many association table - with this solution I'd have a maximum of 1600Koral
200 thousand rows is extremely little. Query should be milliseconds. If you said 1 billion rows different story.Whinny

© 2022 - 2024 — McMap. All rights reserved.