Search code examples
viewcouchbaseemit

Why is it bad to resturn a document from Couchbase View`s


I am trying to enter Couchbase world and learning things about a views. Several time in presentations and demos i heard its bad to return whole doc in from a view:

emit(meta.id, doc);

My question is why? What should i return then and how can i grab a proper values of the document?


Solution

  • It's a bad idea because it's actually counterproductive. Writing a document to the view means it will be stored on disk with the view index itself. You pay the IO price for writing the document to disk again (a duplicate of the original key/value doc), and you pay it again for reading it at query time. Because views queries are served from disk (or the file system cache), you will never take advantage of the integrated cache layer to retrieve the document faster. In short, in average it will be faster to get the document ID from the view and retrieve the document by id, than it is to just read the whole document from the view. This is especially true for operations on multiple documents.