2012-10-23 142 views
1

对不起,有一个很大的问题,但我无法用较少的信息来解释我的情况。
我设计了一个数据库系统,该系统是这样的:文件系统像DBMS中的权限

CREATE TABLE servers(
    ID bigint primary key not null 
    /* other fields of the server */ 
); 
CREATE TABLE producers(
    ID bigint not null, 
    ServerID bigint not null, 
    /* other field of the producer */ 
    constraint "PK_producers" 
     primary key (ID, ServerID), 
    constraint "FK_producer_servers" 
     foreign key("ServerID") references "servers" 
); 
CREATE TABLE records(
    ID bigint primary key, 
    ServerID bigint not null, 
    ProducerID bigint not null 
    /* other fields of record */ 
    constraint "FK_records_servers" 
     foreign key("ServerID") references "servers" 
    constraint "FK_records_producers" 
     foreign key("ServerID", "ProducerID") references "producers" 
); 

CREATE TABLE groups(
    ID bigint not null primary key, 
    GroupName nvarchar(50) not null, 
    Permissions int not null 
    /* other fields of the group */ 
); 
CREATE TABLE users(
    ID bigint not null primary key, 
    UserName nvarchar(50) not null unique, 
    Permissions int not null 
    /* other fields of user */ 
); 
CREATE TABLE users_in_groups(
    UserID bigint not null, 
    GroupID bigint not null, 
    constraint "PK_users_in_groups" primary key (UserID, GroupID), 
    constraint "FK_uig_users" foreign key("UserID") references "users", 
    constraint "FK_uig_groups" foreign key("GroupID") references "groups" 
); 

的数据将被从producers加入records并且每个生产商可以提供500〜2000记录每天每个server通常有2〜4生产,它是完全正常的有3〜6台服务器。正如你所看到的records表增长非常快(我定期存档一些数据和缩小数据库,但records表通常有> 100000条记录)。

现在我的问题是:
正如你看到的用户有不同的权限,根据他们目前属于哪个组以及管理员应用了哪些权限,现在我必须以每个用户可能拥有的方式推进此系统对不同项目(server,producerrecord)的不同权限以如下方式进行:如果用户具有对项目的明确权限,则使用该权限,否则使用来自父项的权限和至少全部用户的权限。例如,用户对record的权限将通过应用于该record,其producerserver或为用户定义的权限的权限指示。

作为第一个解决办法,我想我会实现关系表提供用户与各种对象之间的关系如下:

CREATE TABLE user_server_permissions(
    UserID bigint not null, 
    ServerID bigint not null, 
    Permissions int not null, 
    constraint "PK_usp" primary key ("UserID", "ServerID"), 
    constraint "FK_usp_users" foreign key ("UserID") references "users", 
    constraint "FK_usp_server" foreign key ("ServerID") references "servers" 
); 
CREATE TABLE user_producer_permissions(
    UserID bigint not null, 
    ServerID bigint not null, 
    ProducerID bigint not null, 
    Permissions int not null, 
    constraint "PK_upp" primary key ("UserID", "ServerID", "ProducerID"), 
    constraint "FK_upp_users" foreign key ("UserID") references "users", 
    constraint "FK_upp_server" foreign key ("ServerID") references "servers" 
    constraint "FK_upp_producer" foreign key ("ServerID", "ProducerID") references "producers" 
); 
CREATE TABLE user_record_permissions(
    UserID bigint not null, 
    RecordID bigint not null, 
    Permissions int not null, 
    constraint "PK_urp" primary key ("UserID", "ServerID"), 
    constraint "FK_urp_users" foreign key ("UserID") references "users", 
    constraint "FK_urp_record" foreign key ("RecordID") references "records" 
); 

但是,使用这种方法确实降低性能,因为我与它的工作主要表为records并且管理员为记录设置特殊权限的情况非常罕见,大多数记录应该使用其producer中的权限,但是使用这种技术我应该为表records表的每次访问检查多个表。例如,一个简单的查询,如:

SELECT * FROM "records" WHERE /* Some condition */ AND record_is_accessible("ID") 

将杀死我应该能够响应多个客户端的服务器!
我的大多数客户都使用MSSQL作为DBMS,但很少有使用MySQL或ORACLE的情况。

现在任何人都可以指导我完成任务!

+0

什么* kind *的SQL? – podiluska

+0

请发布一个会“杀死”你的服务器的查询的例子。 –

回答

2

乍一看,一个快速解决方案将是 没有3个单独的权限表。相反,你会有这个

CREATE TABLE user_entity_permissions(
    UserID bigint not null, 
    EntityID bigint not null, 
    EntityType int not null, 
    Permissions int not null, 
    constraint "PK_usp" primary key ("UserID", "EntityID"), 
    constraint "FK_usp_users" foreign key ("UserID") references "users" 
    --- Third FK constraint --- 
    ); 

现在你要相应地塑造你的三键约束(如果需要) 例如让我们说你给服务器的的EntityType = 1,生产者 - > 2,记录 - > 3 然后ENTITYID和的EntityType = 1个引用服务器等等...

这种方式,您还可以使用的EntityType排序您的权限的优先级(例如1第一,然后2,然后3)

+0

非常感谢您的快速回答,我会检查它,并让您知道您的方法的差异,我测试了像您的解决方案,但我不知道为什么我放弃它。我认为我有一些问题,或者我很愚蠢,没有任何理由就忽略它! :) – BigBoss

+0

没问题!如果你绊倒另一个问题,请告诉我。我很乐意与你一起工作。 –