简体   繁体   English

不同角色用户的最佳表结构

[英]Best table structure for users with different roles

We are working on a website which will feature about 5 different user roles, each with different properties.我们正在开发一个网站,该网站将包含大约 5 个不同的用户角色,每个角色都有不同的属性。 In the current version of the database schema we have a single users table which holds all the users, and all of their properties.在当前版本的数据库模式中,我们有一个单独的users表,其中包含所有用户及其所有属性。

The problem is that the properties that we need differ per user role.问题是我们需要的属性因用户角色而异。 All users have the same basis properties, like a name, e-mail address and password.所有用户都具有相同的基本属性,例如姓名、电子邮件地址和密码。 But on top of that the properties differ per role.但除此之外,每个角色的属性也不同。 Some have social media links, others have invoice addresses, etc. In total there may be up to 60 columns (properties), of which only a portion are used by each user role.有些有社交媒体链接,有些有发票地址等。总共可能有多达 60 列(属性),每个用户角色只使用其中的一部分。

In total we may have about 250,000 users in the table, of which the biggest portion (about 220,000) will be of a single user role (and use about 20 of the 60 columns).表中总共可能有大约 250,000 个用户,其中最大的部分(大约 220,000)将是单个用户角色(并使用 60 列中的大约 20 个)。 The other 30,000 users are divided over four other rules and use a sub-set of the other 40 columns.其他 30,000 名用户分为四个其他规则,并使用其他 40 列的子集。

What is the best database structure for this, both from a DB as a development perspective?从数据库作为开发的角度来看,最好的数据库结构是什么? My idea is to have a base users table, and then extend on that with tables like users _ moderators , but this may lead to a lot of JOIN'ed queries.我的想法是有一个基本的users表,然后使用users _ moderators 之类的表对其进行扩展,但这可能会导致很多 JOIN 查询。 A way to prevent this is by using VIEWs, but I've read some (out-dated?) articles that VIEWs may hurt performance, like: http://www.mysqlperformanceblog.com/2007/08/12/mysql-view-as-performance-troublemaker/ .防止这种情况的一种方法是使用 VIEW,但我读过一些(过时的?) VIEW 可能会损害性能的文章,例如: http : //www.mysqlperformanceblog.com/2007/08/12/mysql-view -作为性能麻烦制造者/

Does the 'perfect' structure even exist? “完美”的结构是否存在? Any suggestion, or isn't this really a problem at all and should we just put all users in a single big tables?有什么建议吗,或者这根本不是问题,我们是否应该将所有用户放在一个大表中?

There are two different ways to go about this.有两种不同的方法可以解决这个问题。 One is called "Single Table Inheritance".一种称为“单表继承”。 This is basically the design you ask for comments on.这基本上是您要求评论的设计。 It's pretty fast because there are no joins.它非常快,因为没有连接。 However NULLs can affect throughput to a small degree, because fat rows take a little longer to bring into memory than thinner rows.然而,NULL 会在很小的程度上影响吞吐量,因为胖行比薄行需要更长的时间才能进入内存。

An alternative design is called "Class Table Inheritance".另一种设计称为“类表继承”。 In this design, there is one table for the super class and one table for each subclass.在这个设计中,超类有一个表,每个子类有一个表。 Non key attributes go into the table where they pertain.非关键属性进入它们所属的表。 Often, a design called "Shared Primary Key" can be used with this design.通常,这种设计可以使用称为“共享主键”的设计。 In shared primary key, the key ids of the subclass tables are copies of the id from the corresponding row in the superclass table.在共享主键中,子类表的键 id 是超类表中相应行的 id 的副本。

It's a little work at insert time, but it pays for itself when you go to join data.这是插入时的一点工作,但是当您去连接数据时,它会为自己付出代价。

You should look up all three of these in SO (they have their own tags) or out on the web.您应该在 SO(它们有自己的标签)或网上查找所有这三个。 You'll get more details on the design, and an indication of how well each design fits your case.您将获得有关设计的更多详细信息,以及每个设计与您的情况的匹配程度的指示。

'Perfect' structure for such cases, in my opinion, is party-role-relationship model.在我看来,这种情况的“完美”结构是政党-角色-关系模型。 Search for Len Silverston's books about data models.搜索 Len Silverston 关于数据模型的书籍。 It looks quite complicated at the beginning, but it gives great flexibility...一开始看起来很复杂,但它提供了很大的灵活性......

The biggest question is practicability of adopting perfect solution.最大的问题是采用完美解决方案的实用性。 Nobody except you can answer that.除了你,没人能回答。 Refactoring is never an easy and fast task, so say if your project lifetime is 1 year, spending 9 month paying out 'technical debts' sounds more like wasting of time/efforts/etc.重构从来都不是一件容易和快速的任务,所以假设你的项目生命周期是 1 年,花费 9 个月来支付“技术债务”听起来更像是在浪费时间/努力/等。

As for performance of joins, having proper indexes usually solves potential issues.至于连接的性能,拥有适当的索引通常可以解决潜在的问题。 If not, you can always implement materialized view ;如果没有,你总是可以实现物化视图; even though mysql doesn't have such option out of the box, you can design it yourself and refresh it in different ways(for instance, using triggers or launch refresh procedure periodically/on demand).即使 mysql 没有开箱即用的此类选项,您也可以自己设计它并以不同的方式刷新它(例如,使用触发器或定期/按需启动刷新程序)。

table user table roles table permissions table userRole table userPermission table RolesPermissions

Each role have is permissions in role permissions table Each user can have a permission whitout the role (extention...)每个角色在角色权限表中都有权限每个用户都可以拥有一个没有角色的权限(扩展...)

So in PHP you just have to merge arrays of user permissions in user roles and extended permissions... And in your "acl" class you check if your user have the permission to view or process a webpage or a system process...因此,在 PHP 中,您只需要合并用户角色和扩展权限中的用户权限数组……在您的“acl”类中,您检查您的用户是否有权查看或处理网页或系统进程……

I think you don't need to worry about speed here so much.我认为你不必太担心这里的速度。 Because it will be one time thing only.因为这只会是一次。 ie on user login store acl in session and get it next time from there.即在会话中的用户登录存储 acl 并下次从那里获取它。

JOINs are not so bad. JOIN 并没有那么糟糕。 If you have your indexes and foreign keys in right places with InnoDB engine it will be really fast.如果您使用 InnoDB 引擎将索引和外键放在正确的位置,它会非常快。

I would use one table for users and role_id.我会为用户和role_id 使用一张表。 Second table with roles.带有角色的第二个表。 Third table for resources, and one to link it all together + enabled flag.第三个资源表,一个将它们链接在一起+启用标志。

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

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