简体   繁体   English

多租户设计 - 在模式之间共享数据

[英]Multi tenancy design - sharing data between schemas

Let's consider such case:让我们考虑这样的情况:

  1. User can have a company (or many of them)用户可以拥有一家公司(或其中许多)
  2. User can be a part of a company (or in many of them)用户可以是公司的一部分(或其中许多公司的一部分)
  3. Company is a single tenant of the system公司是系统的单一租户
  4. Company has a list of tasks公司有任务清单
  5. Each task is assigned to a user每个任务分配给一个用户

Now given the circumnstaces above, I want to implement a system in which each company (tenant) has a separate schema for its tasks, but the problem is that for each task I also need a user data from the main schema.现在考虑到上述情况,我想实现一个系统,其中每个公司(租户)都有一个单独的任务模式,但问题是对于每个任务,我还需要来自主模式的用户数据。

The question is how to approach this problem问题是如何解决这个问题

The possible solutions I have thought of (but none is really convincing me):我想到的可能的解决方案(但没有一个真正让我信服):

  1. Copying all users data matched with a company to company's schema (it would require a fair amount of synchronization, therefore I don't find it very efficient)将与公司匹配的所有用户数据复制到公司的架构(这需要大量的同步,因此我认为它不是很有效)
  2. Switching between schemas and 'merging' them programmatically - this one involves a lot of additional code to implement and it violates good practices - as the user_id in task would reach outside the tanant's schema)在模式之间切换并以编程方式“合并”它们 - 这涉及许多额外的代码来实现,它违反了良好的做法 - 因为任务中的 user_id 将到达 tanant 的模式之外)

I hope there is a better solution I haven't thought of.我希望有一个我没有想到的更好的解决方案。 Please note that this is a simplified case, just to describe the problem.请注意,这是一个简化的案例,只是为了描述问题。

It sounds like you want a handful of tables, like users , companies , tasks , and related tables.听起来您想要一些表,例如userscompaniestasks和相关表。

In general, you do not want to split entities across multiple tables.通常,您不希望跨多个表拆分实体。 Here are some reasons.以下是一些原因。

  1. It is easier to maintain a handful of tables rather than hundreds or thousands of tables.维护少数表比维护成百上千个表更容易。
  2. Databases are more efficient on larger tables.数据库在较大的表上有效。 A proliferation of small tables results in lots of partially filled data pages for a single entity.小表的激增导致单个实体出现大量部分填充的数据页。
  3. Certain queries -- such as how many tasks are in each company -- are much easier with a single table.某些查询——例如每家公司有多少任务——使用单个表要容易得多。
  4. With multiple tables, such queries often require resorting to dynamic SQL, which is just a mess for simple tasks.对于多个表,这样的查询通常需要求助于动态 SQL,这对于简单的任务来说只是一团糟。
  5. Restructuring the data becomes a nightmare when you have to apply the restructuring to a zillion tables rather than a handful of tables.当您必须将重构应用于数以万计的表而不是少数表时,重构数据就变成了一场噩梦。
  6. Adding new features is a nightmare when it has to be repeated multiple times.添加新功能是一场噩梦,因为它必须重复多次。

There are some rare circumstances where it makes sense to separate data.在一些罕见的情况下,分离数据是有意义的。 For instance, if the application is going to be on-premise at each company, then you have no choice.例如,如果应用程序将在每个公司内部部署,那么您别无选择。 Similarly, you might have legal requirements for keeping data physically separate.同样,您可能有将数据物理隔离的法律要求。 But from a strict database-design perspective, you want one table per entity.但是从严格的数据库设计角度来看,您需要每个实体一个表。

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

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