3

我正在尝试进入 Couchbase 世界并学习有关视图的内容。有几次在演示和演示中,我听说从视图中返回整个文档是不好的:

emit(meta.id, doc);

我的问题是为什么?那么我应该返回什么以及如何获取文档的正确值?

4

2 回答 2

6

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.

于 2015-01-15T19:39:52.347 回答
2

这很糟糕,因为它会大量消耗资源,视图经常会更新和覆盖索引,因此如果您重复编写整个文档,它将需要大量的处理器时间和磁盘 I/O(以及文件系统缓存)。

因此,建议(并且效率更高)让视图返回 doc.id,然后使用标准get过程返回整个文档。

于 2015-01-15T12:12:08.857 回答