Ali Kahoot
Ali Kahoot

Reputation: 3579

Should I create more documents or create less Documents with arrays in Azure DocumentDB?

Should I create a separate document for each entry of my DB in DocumentDB or should I create a single document and create an array with all the elements?

e.g. If I have around 30k-40k products which are differentiated on the basis of categories, so should I make 30k documents, each for product, or should I make a document per category and add the products in that category document's array?

I want to know both the performance and cost factor.

Upvotes: 0

Views: 57

Answers (1)

David Makogon
David Makogon

Reputation: 71026

There's no "right" answer to document modeling, cost, and performance, as it is all very dependent on your app & data, but from an objective perspective:

When you have an unbounded array, you will eventually exceed the maximum size limit of a document. And then your data model will be broken.

Further, the larger the array, the more costly it will be for you to manipulate the document containing the array (e.g. updates to add more items).

If, say, you had 40,000 products (per your question's description): If you stored these in an array, you'd need to query the array every time, vs just pulling the product's document. Also: how large is a product (id, description, links to images, etc)? The larger that info, the more storage it consumes in your document.

Also: as the max document size is 2MB: If you had 40,000 products, you'd only have room for about 50 bytes per product.

If, on the other hand, you had one document per product, you could easily query for a specific product. You can have a very large product description without fear of overrunning max document size. You'd be able to have arrays of product id's (if needed somewhere) within another document (vs an array of product documents).

As to your question on cost, efficiency, etc: only you can determine that part, via a bit of benchmarking (and examining RU costs per insert/read/query).

Upvotes: 1

Related Questions