Solved

Bit mask advantages/disadvantages

Posted on 2009-04-15
19
940 Views
Last Modified: 2012-05-06
Hi guys,

just wondering if you could give me your input on the usage of a bit-mask field for storing user access rights, rather than having a separate table. Yes I know it is not normalized. Input on indexing is also appreciated.

Database: SQL Server

Thanks in advance
0
Comment
Question by:miral13
  • 6
  • 5
  • 4
  • +1
19 Comments
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
Not exactly sure what you mean...can you give an example?
0
 

Author Comment

by:miral13
Comment Utility
Let's say I have the rights stored in a table
1 - Home
2 - Products
4 - Downloads
8 - Forum

And I have users in another table, with their access rights
Michael - 15 (access to everything)
Laura - 3 (only Home and Products)

We can use the bitwise operators to query this.

What would be the disadvantages of this solution rather than having a table with the following associations?
Michael - Home
Michael - Products
Michael - Downloads
Michael - Forum
Laura - Home
Laura - Products

0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
OK...I gotcha now.  To be honest, I've always kinda liked this idea.  The only tricky thing is if you ever need to do any joins basped on that data...it makes it more difficult to look up.  however, if you don't have that many values, then it might work out.
0
 
LVL 39

Accepted Solution

by:
BrandonGalderisi earned 100 total points
Comment Utility
The downside would be that if you want to do a search for everyone who has the downloads privilege, it will not be able to do an indexed search.

Your bitwise would be

select * from users
where UserPrivColumn & 4=4

But it would have to do the bitwise operator against every record to determine if the 4 bit is set.

Alternatively, you can say.

select userid from userprivs where privname = 'downloads'

What you could consider is a hybrid approach.  Since from the application perspective it would be nice to contain a single value of everything, and the database is more search-able with individual values, store it both ways.  Just make sure that the value can only ever be changed via a stored proc so that your logic is in one place.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>just wondering if you could give me your input on the usage of a bit-mask field for storing user access rights<<
We use this approach extensively. We have never had to do this, but if you do notice some slowness you can create computed columns on the bit column to represent the all the options:
1 - Home
2 - Products
4 - Downloads
8 - Forum
You can then index those columns.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
Slightly off-topic, but it is mind blowing what a hard time I have had explaining this subject at work:  People just don't get it.  To the point that I have had to write functions just for extracting the values into "readable" format.
0
 
LVL 60

Assisted Solution

by:chapmandew
chapmandew earned 50 total points
Comment Utility
also, kinda off-topic, but a way in which you can parse out your bitmask values (an article I wrote for auditing)

http://articles.techrepublic.com.com/5100-10878_11-6175865.html
0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 100 total points
Comment Utility
Thanks for that.  sys.fn_IsBitSetInBitmask looks like an interesting function.  I need to look into it.
0
 

Author Comment

by:miral13
Comment Utility
I already had this article  http://sqlfool.com/2009/02/bitwise-operations/ which covers the sujbect much better. But I am looking for input from people who actually used this and found any disadvantages and how they managed the indexing.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
It looks like that has been covered.  What else were you looking for?
0
 

Author Comment

by:miral13
Comment Utility
As I said. Indexing and disadvantages from other users' experience
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
I suspect that if you read the comments posted, both Tim, Brandon and I covered that and possible workarounds.
0
 

Author Comment

by:miral13
Comment Utility
I am looking for new information.  Something based on real experience with bit mask fields. I keep empathising  on the keyword disadvantages. I want to know if somebody has implemented and what was the impact on performance. I am sorry but I don't think that this question received all the possible answers yet. Have a nice weekend.
0
 
LVL 60

Expert Comment

by:chapmandew
Comment Utility
Actually, it has....you're received answers for advantages and disadvantages.
0
 
LVL 75

Expert Comment

by:Anthony Perkins
Comment Utility
>>Something based on real experience with bit mask fields<<
Yes we gave that to you.

>> I keep empathising  on the keyword disadvantages.<<
And that was covered when Tim discussed Joins and Brandon focused on searching.

>>I want to know if somebody has implemented and what was the impact on performance.<<
We have all told you we have.  I added that in our case the impact was minimal and suggested workarounds if this was not your case.
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
Comment Utility
Normally I dispute poor grades.  Normally those poor grades are Bs.  You got good information from 3 TOP experts on EE and you dismiss is because you are unwilling, or unable, to comprehend the answers provided.
0
 

Author Comment

by:miral13
Comment Utility
Sorry Brandon because I upset you. Please check this thread http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=59778
In the end it provided more info than the replies posted here. And yet I closed the question.
You can choose to be upset or you can understand that there was more feedback that could have been provided.

0
 
LVL 39

Expert Comment

by:BrandonGalderisi
Comment Utility
You asked for "OUR EXPERIENCES".  We gave you our experiences.  Would you have preferred that we made something up?
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now