PDA

View Full Version : Efficiency Question


Aaron
10-07-2003, 07:38 AM
I've developed a permissions system as part of a the admin side a website. There are dozens of possible permissions which can be granted to a user, either individually or as part of a group. On any given page load, up to five of these permissions may be checked for. Would it be better to store the permissions in a table with the form:

ID User Permission
1 1 1
1 1 2
1 1 3
1 2 7


or


ID User Permission
1 1 1,2,3
1 2 7
1 3 10,42


And just get the the whole row and search in the permission string for the desired permission number?

Thanks

------------------

Giraffe
10-08-2003, 10:26 AM
Assuming it's a on/off permission thing, you'd do better packing them all into an integer, then doing bitmask tests to see which bits were set.

0 = no bits set
1 = first bit set
2 = second bit set
3 = first and second bit set
...
31 = first 5 bits set

etc.

[Edit: I assume that's MySQL? If so... [URL=http://www.mysql.com/documentation/mysql/bychapter/manual_Reference.html#Bit_functions]http://www.mysql.com/documentation/mysql/bychapter/manual_Reference.html#Bit_functions[/ URL] ]
[Edit: To test for the nth bit set, Bitwise OR the number with 2^n (i.e. 1, 2, 4, 8, 32, ... - that's n starting at 0 btw)]

[This message has been edited by Evil_Giraffe (edited October 08, 2003).]

[This message has been edited by Evil_Giraffe (edited October 08, 2003).]

Dormouse
10-08-2003, 10:53 PM
Gir beat me to it, listen to him.

------------------
Blue Mink Bifocals ! (http://dormouse.spyw.com/)
fsck -Rf /world/usr/ (http://deadkittyp.com/)
Capite Terram (http://villainsupply.com/)
"Stupid English language... Why does nothing rhyme with 'primordial goo'???" -happydud
NPC.Interact::PressButton($'Submit');

Aaron
10-10-2003, 01:06 PM
I like the bitwise operations, that would be much better than either system (in several ways). I'm actually using SQL server, but I may aswell just get the value and let the script handle the operators.

Thanks for the help.

------------------