LivingOnACloud
LivingOnACloud

Reputation: 1261

How should I store lots of rows in DocumentDb

I have an entity in my system which will have a lot of comments added against it in a short period of time.

Would I be correct in saying that if I read a document and then modify something in it, the entire object is then persisted back to the store?

If this is correct, then loading and storing an object with 5000 comments just so that I can add a comment seems like a bit too much?

Should I store each comment as a document and then scan through the document collection for a particular key? I would also need to be able to quickly find a comment and modify it.

Upvotes: 3

Views: 463

Answers (1)

b2zw2a
b2zw2a

Reputation: 2693

One massive document

If you would like to store all comments in one document you will run into few issues:

  • Concurrent writes – you will have to do a lot of read and retry operations (non-current Etag)
  • Loading large document to add a comment
  • Loading large document to edit a comment

Pros:

  • You can delete main object with all documents in one operation

Multiple collections

The best option I guess is splitting comment and main objects into two collections and link every comment to the main object by id. This way you can add and edit single comment as well as retrieve all documents by query. This way you can run much more complex queries on comments collection for instance - get all comments by user etc.

Cons:

  • Deleting main object with all comments will be quite expensive (you have to delete comment one by one)

Although you have to be aware that OrderBy clause is not supported yet, so you might have a problem retrieving for example last 10 comments etc.

Possible alternative

Not sure what are the other operations/queries you have to support. But if it’s only CRUD, retrieving list of comments for main object and potentially getting last x comments, you could consider using Azure Table where:

  • Row key – Time based identifier (Unix epoch ) + GUID to avoid collisions
  • Partition key – main object id

This way you can retrieve and update comment entity extremely fast and as well as query it by time

Upvotes: 1

Related Questions