Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 905
ArkadyB Avatar asked Dec 01 '25 03:12

ArkadyB


2 Answers

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.

like image 143
David Ostrovsky Avatar answered Dec 05 '25 08:12

David Ostrovsky


It's bad because it's a large drain on resources, views will often update and overwrite indices, so if you are writing a whole doc repeatedly it's going to require a large amount of processor time and disk I/O (along with filesystem cache).

Therefore, it is recommended (and far more efficient) to have the view return the doc.id and then use the standard get procedure to return the whole doc.

like image 38
mrkwse Avatar answered Dec 05 '25 09:12

mrkwse



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!