Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multitenant DB: Why put a TenantID column in every table?

Every tutorial I've seen about Multitenant database models tells you to put the TenantID in every single table:

zoos
-------
id
zoo_name
tenant_id

animals
-------
id
zoo_id
animal_name
tenant_id

However, this seems redundant to me. Why not add the tenant_id column to just the zoos table and exploit the foreign key relationship between zoos and animals?

Do you add tenant_id to every table just to keep the joins from getting too crazy? Is it a safeguard against bugs? A performance consideration?

like image 852
Mike Sickler Avatar asked Nov 16 '09 15:11

Mike Sickler


People also ask

What is the benefit of developing application in multitenant environment?

A multi-tenant application can provide savings by reducing development and deployment costs to companies that develop applications. These savings can be passed on to customers – increasing competitive advantages for all parties involved.

What is database multi-tenancy?

Multi-Tenant – Multi-tenancy means that a single instance of the software and its supporting infrastructure serves multiple customers. Each customer shares the software application and also shares a single database. Each tenant's data is isolated and remains invisible to other tenants.

What is tenant in DB?

A multi-tenant database is a database that provides support to a number of separate and distinct groups of users, referred to as tenants.

What is multi-tenancy in rails?

Multitenancy means serving multiple independent customers from one app. Pretty typical for SaaS model. You can implement it on several different levels: Row level - you put a tenant_id column into every DB table and filter by tenant_id in every query.


1 Answers

If one of your key design considerations is security--specifically, one client can no way no how no when access another client's data--then, depending on how you implement this security, sticking that qualifying column in every table may be necessary. One such tactic described here requires building a view on every table; assuming each table contains a tenantId column, then if properly configured each view could contain a "WHERE tenantId = SUSER_SID()" clause (and of course you configure the database so that clients can only access the views).

Another factor (as in my current job) is loading warehouse data (ETL). Tables are partitioned on tenantId (we use table partitioning, but partitioned views would also work), and data can be easily loaded or unloaded for a client without seriously impacting any other client.

But as ever, there's a lot of "it depends" involved. If there is no clear and present need, and a very low likelihood of future need, then normalize that column out. Just realize that it's more a devise of physical implementation than of conceptual or logical database design.

like image 52
Philip Kelley Avatar answered Sep 28 '22 09:09

Philip Kelley