Sylvain
Sylvain

Reputation: 19249

Should we denormalize data in a Document to improve performance? (Document Database)

In my system Employees log Requests and Requests are about an Equipment.

In order to avoid loading the Employee and Equipment documents each time I need to display a Request I want to denormalize the employee name and the equipment inventory number, manufacturer name and model in the Request document.

I’m I on the wrong track here? Is this an antipattern?

I realize that if I do that then I’ll have to update all affected Request documents in the very rare case that an employee’s name changes or an equipment’s inventory number changes.


PS: Links to Document Database Modeling Guidelines would be appreciated as well.

Upvotes: 2

Views: 425

Answers (3)

Ayende Rahien
Ayende Rahien

Reputation: 22956

Sly, In your case, do you really need to update the information if it changes? If Sally May requested a new laptop in 2011, is it meaningful to change that request to say Sally May-Jones in 2012 ?

Usually when we want denormalization, we want it not for performance (although it is a factor, Include can deal with that nicely). We want that to get point-in-time view of the data.

Upvotes: 1

rob05c
rob05c

Reputation: 1233

Here is a good, short post on denormalization.

In summary, denormalization is ok if you're never going to update, and the performance advantage outweighs the storage disadvantage (it usually does).

Upvotes: 1

Daniel Lang
Daniel Lang

Reputation: 6839

Sly, this really depends on your required level of speed. If you just have one database without sharding and you're ok with very fast performance, then don't denormalize and use .Include for the sake of simplicity. However, they don't work on sharded sessions, so you might want to go for denormalization then and have lightning performance.

My very personal opinion is that you should always go with .Include unless you have a really good reason not to use it.

Upvotes: 2

Related Questions