简体   繁体   English

在mysql数据库中,哪种保存权限的方法更有效?

[英]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. 在我的mysql数据库中,我有一个权限表,其中存储了权限名称和权限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. 我有一个用户表,其中存储了用户名,密码等,其中还包含一个ID,并且我有一个组表,其中存储了组名和组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: 要制作2个表,一个表包含用户权限,另一个表包含组权限,因此如下所示:

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. 即使在一个表中速度更快,我还是要两个:数据完整性比浪费几个µs的处理器时间更重要,但快速访问不可靠或损坏的数据并没有多大意义。

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. 再仔细考虑一下,用第二种方法做的任何潜在收益都不值得,IMO。 There may be some third way, but of the two you posted the first is superior. 可能有第三种方法,但是在您发布的两种方法中,第一种方法更优越。 Simple, clean, and fast. 简单,干净,快速。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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