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?
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.
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.
A multi-tenant database is a database that provides support to a number of separate and distinct groups of users, referred to as tenants.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With