22

I have a web app that I built for a trucking company that I would like to offer as SaaS. What is the best way to design the database?

Should I create a new database for each company? Or should I use one database with tables that have a prefix of the company name? Or should I Use one database with one of each table and just add a company id field to the tables? Or is there some other way to do it?

givanse
  • 14,503
  • 8
  • 51
  • 75
Josh Curren
  • 10,171
  • 17
  • 62
  • 73
  • 1
    What you've stated is too vague. What does 'Company' represent in your application? – Randy Minder Feb 04 '10 at 18:16
  • @Randy - A company is a group of users who have access to the same data. So Trucking Company A may have 5 users who access their data. Trucking Company B may have 20 users accessing a completely different set of data. Does that help? – Josh Curren Feb 04 '10 at 18:30
  • The more common abbreviation is "SaaS". Normally, SAS refers to SAS, inc. http://www.sas.com/. It's not clear if you're talking about the company or the services offering idea. You might want to fix your question. – S.Lott Feb 04 '10 at 19:07
  • 1
    Database design and schema design questions are off-topic at Stack Overflow. Perhaps http://dba.stackexchange.com/ would be a better choice. – jww Feb 06 '14 at 07:31
  • possible duplicate of [What are the advantages of using a single database for EACH client?](http://stackoverflow.com/questions/13348/what-are-the-advantages-of-using-a-single-database-for-each-client) – givanse Feb 06 '14 at 08:35
  • 2
    I'm amused that people believe that there can be a single correct answer to this question, and furthermore it's potentially damaging to nominate one approach as the correct one. @jww was right that this should have been voted as off-topic -- there are too many considerations for there to be a single answer. – David Aldridge Nov 12 '15 at 20:54

4 Answers4

31

faced with a similar situation about 10 years ago, we opted for a database per client. we have hundreds (not thousands) of clients. looking back it was one of the best decisions we made. backups are easy. copying a single client to our office for analysis is easy (just take the last backup). scaling is easy (moving a single big client to a different server can free up resources on a stressed sql server). joel & jeff had a discussion about this on a stack overflow podcast (not a recent one) and joel did the same thing i do ... each client gets their own database. database purists will often argue for lumping everyone into one db, but i would never do that.

-don

Community
  • 1
  • 1
Don Dickinson
  • 6,200
  • 3
  • 35
  • 30
  • 2
    i should add that the databases themselves are named for the client. the tables in all databases are named exactly the same. – Don Dickinson Feb 04 '10 at 18:17
  • 3
    How do you maintain this? What if schema changes -- do you have to change all databases' schemas? – Allen Gingrich May 02 '14 at 04:18
  • Hmmm, which of the following poplar SaaS offerings use one database per client? Trello, Basecamp, Google Apps, Salesforce CRM, Freeagent, Xero, Workday ... – David Aldridge Nov 12 '15 at 21:02
  • 1
    One database per client would be a operations/maintanence nightmare if you have a sizable clientbase - so dont do it if you dont have a dedicated DB team. – Dharmendar Kumar 'DK' Apr 14 '16 at 03:30
  • this is wild..so you have thousands of databases? wow...so when a user signs up, you create a new database for them? will love go get more details about this design – uberrebu Jul 13 '22 at 10:28
11

Should I create a new database for each company?

Yes - Don Dickinson was on the money. However, see a refinement below.

Or should I use one database with tables that have a prefix of the company name?

Lord no! Changing your database queries for different for client would make you go insane! Also, you'd almost certainly run dynamic SQL (where the table name is changed in code before running the query), which would harm performance as most servers like to cache query plans and interim results - this doesn't work if the table names keep changing.

Or should I Use one database with one of each table and just add a company id field to the tables?

You might want to do this if you want to have some kind of scalable model for your customers. Whilst provisioning a new database for each customer gives you lots of flexibility, it also involves costs and complexity. You have to create a new backup schedule, have a lifecycle model for dealing with expired customers etc.

So, you might say that "free trial" and "bronze" customers are all lumped into a single database, using the company id to separate them out; "silver" users get their own database (but you still keep the customer_id field in the schema, so you don't have to change queries between two levels of customer), and "gold" customers get their own database server.

I did something similar a few years ago at a SaaS company - and customers are typically happy to have an upgrade path on infrastructure (read: performance and resilience) as well as features.

Neville Kuyt
  • 29,247
  • 1
  • 37
  • 52
3

We have some databases here with shared clients and some where each client has it's own server and own database. The ones where the client is on it's own server are the easiest to manage and the least likely to cause a problem when some developer forgot to add the clientid and sent client a's data to client b by accident (an example NOT chosen at random).

Keeping each on it's own server or server instance allows us to keep the database structure the same with the same names and makes it easier to propagate changes to all the servers because we don't have to change the database name.

If you do use separate instances for each client, make sure you design and implement a good system for propagating all changes to all clients. If these databases get out of sync, they can become horrible to maintain. You'll find that if you let them get out of sync, each client will ask for changes and you will have 27 ways to do the same thing. You have to generalize when they are on the same database, when they are separate you have to use self discipline to ensure new functionality is the same for each client.

WorkSmarter
  • 3,738
  • 3
  • 29
  • 34
HLGEM
  • 94,695
  • 15
  • 113
  • 186
0

It depends, here, i work in a company that has many "Internal Business units" treated like other companies. So, some reports must include all companies, Client accounts must also be shared across companies. Here we have a CompanyId Field in the tables that requires it. The Prefix solution is surely one to be avoided.

Burnsys
  • 854
  • 1
  • 8
  • 11