简体   繁体   中英

which way of saving permissions is more efficient in a mysql database?

I want to save permissions for both individual users, and user groups. In my mysql database, I have a permissions table where I store a permission name, and a permission id. I have a user table, where I store a username, password etc. which also contains an id, and I have a groups table which stores a group name and group id.

What would now be the most efficient option? To make 2 tables, one containing user permissions and one containing group permissions, so something like this:

int group id | int permission id

int user id | int permission id

or would it be better to have one table like this;

int id | int permission id | enum('user','group')

I would recommend using two tables.

First you can reduce the seeking time by checking the first table for group permission and if there is no group permission to search for user permission.

Second it will be easier to understand by other programmers.

I doubt there would be much of a performance difference between your two approaches; but, as with all performance and optimization questions, feelings and guesses don't matter, only profiled results matter. Which one will be more efficient depends on your data, your database, your access patterns, and what "efficient" means (space? time? developer effort? final monetary cost?) in your context.

That said, using two tables is a better structure as it allows you to have foreign keys from your group-permission table back to your group table and your user-permission table back to your user table. Even if it was faster in one table I'd still go with two: data integrity is more important than wasting a couple µs of processor time, I don't see much point in quickly accessing unreliable or broken data.

The first way looks more time-efficient, the second way looks more space-efficient. For the speed of a unique index, in the 2nd case you'd have to index on the 1st and 3rd fields.


Mulling over it a little more, any potential gains of doing it the 2nd way aren't worth it, IMO. There may be some third way, but of the two you posted the first is superior. Simple, clean, and fast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM