Nest
Nest

Reputation: 341

CouchDB Document modelling

[TLDR] Does CouchDB cache and reuse the results of map/reduce functions for non-modified documents?

Background info: In my Point of Sale (POS) application, all the transactions are logged and created separate document.

{ "_id": "sale_transactions_1234124", "Quanity Added": 0, "Quanity reduced": 10, "Is Discarded": "false", "saleid": "sales_523" }

{ "_id": "Purchaseorder_transactions_1234124", "Quanity Added": 5, "Quanity reduced": 0, "Is Discarded": "false" , "purchaseid": "purchase_2352"}

Whenever the inventory is modified, relevant documents are created as transactions it is _id field. So instead of store Total sold, Quantity in hand in Item document, have plan to create map reduce view to retrieve every time.

Every time when I make a sale, Quantity in hand map reduce function should be called, to check whether the quantity is greater than zero. For this approach will I encounter any performance issue, or did you recommend create Total sold, Quantity in hand in Item document

So every time we call map reduce function, is couchdb some how cache non-modified document results and process new and modified documents, or is it process all the documents in every time?

Upvotes: 1

Views: 98

Answers (1)

Joshua Beckers
Joshua Beckers

Reputation: 907

Yes CouchdDB caches your map/reduce documents. So if you query the same view multiple times Couch will give you the cached version. If additional datasets are added to the database Couch will update the view.

To see that Couch caches the views you can try to create a new view on a database with a lot of documents:

  1. Call the view for the first time and you will see that it takes some time to get your result.
  2. Call your view again and it will be ready immediately.
  3. Add a few new documents to your database
  4. Call your view and it will be ready very fast if not immediately.
  5. Add a lot of new documents to your database
  6. Call your view again and it will take more time than when you just added a few new documents. But will be ready immediately after it is build up.

Upvotes: 0

Related Questions