Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am creating a RESTful API. I am struggling to decide on the best way to design my database tables around my resources.

Initially, I though a table per resource would be a good way to go, but I'm now worried that this will result in exponentially bigger tables the further down the resource chain you go.

For example, imagine I have three resources - users, clients, sales. Users are subscribers to my api, clients are the users customers, and sales are purchases made by each client to the users account.

A sale resource is accessed as follows

GET /users/{userID}/clients/{clientID}/sales/{salesID}

So if there are 10 users, each with 10 customers, and for each customer there are 10 sales, the table size gets larger the further down the resource chain we go.

Im fairly confident that SQL can cope with large tables, but I'm not sure how read and writes will slow things down. The example above maybe doesn't illustrate it, but my api will have progressively more writes and reads the further down the resource chain we go. I therefore have the scenario where the biggest tables in my database, will be read and written to more times than smaller tables.

It will also be necessary to join tables before running queries. The reason is that I allow each user to have a client with the same name. To avoid getting the wrong client data, the users table and clients tables are joined by {userID}. This is also the case for sales. Will joining large tables and running reads and writes slow things down further?

share|improve this question
up vote 28 down vote accepted

I am struggling to decide on the best way to design my database tables around my resources.


Design your API according to RESTful principes, design your database according to normalisation principles. One does not need to impact upon the other.

Your database should not contain a SaleResource table, it should contain a Sale (or purchase/order) table. That table will include a primary key that uniquely identifies a Sale and foreign keys to related User and Customer tables.

You REST api will translate a request for the resource identified by GET /users/{userID}/clients/{clientID}/sales/{salesID} to the appropriate database query, retrieve the row, construct the resource that represents a Sale and return it to the client.

Be mindful that you are currently exposing what appears to be internal database identifiers (UserID/ClientId/SalesID) to the outside world. It may be appropriate in your case but generally <entity>ID feels off in a RESTful API.

share|improve this answer
thanks. So what you're basically saying is that so long as i normalise my databases and setup appropriate indexing etc, there should be no performance problems for what i want to achieve – Gaz_Edge Feb 18 '13 at 18:16
Yes. Nothing you've mentioned suggests deviating from a normalised schema would be necessary. – Mark Storey-Smith Feb 18 '13 at 18:26

Relational databases (hence SQL) are really good at locating one (or few) rows from huge table. This is what indexes are for. They're also pretty awesome at handling joins. You don't really have a question. Between the lines, you're basically asking about how to do a multi-tenant database design. I suggest you read Multi-Tenant Data Architecture before asking further questions. Choose one of the patterns (separate database, shared database separate schemas, shared database shared schema) and then we cna discuss specifics. Right now you only envision the last pattern, but did not consider the pros and cons. Read up.

As a side note: you don't need user/{userID} in the URI. You know the tenant (userID) from the authentication info.

share|improve this answer
thanks- Yeah i do need to read up further. My projects to date have had very little database work. I will read the resource you recommended – Gaz_Edge Feb 18 '13 at 16:05
I think shared-shared is the way to go for me. My 'users' are not 'tenants'. They do not require isolated data and will never need direct access to any database management functions. From what I read, this would suggest shared-shared is best? What do you think? – Gaz_Edge Feb 18 '13 at 18:17
shared is the easiest. You must add the user_id as a key to every table and add left.user_id = right.user_id to relevant joins (usually, all). Some ORMs support scopping the access. And don't be fooled, your users are tenants, since you don't want user 'foo' to see/modify the sales of user 'bar'. – Remus Rusanu Feb 18 '13 at 18:35
Thanks. Im beginning to see that indexes are key to creating my database structure. – Gaz_Edge Feb 18 '13 at 19:10

Just to add to what has already been said here - you may want to build an interface between the actual API and your database layer. It makes adding caching and summary tables easier down the line...

share|improve this answer
some links or further explanation would be helpful – Gaz_Edge Feb 19 '13 at 10:04
Sorry, can't comment yet.. – Matt Koskela Feb 19 '13 at 18:37

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.