为什么CouchDB文档建议不应在视图中发出文档? [英] Why is the CouchDB documentation suggesting docs should not be emitted in the view?

查看:45
本文介绍了为什么CouchDB文档建议不应在视图中发出文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在第一个示例下的CouchDB官方文档的查看归类一章中( http://docs.couchdb.org/en/1.6.1/couchapp/views/collat​​ion.html#views-collat​​ion ),建议您不要发布在视图中显示文档本身,建议在请求视图时通过使用?include_docs = true 请求视图来包括文档的正文。

In the View Collation chapter of the CouchDB official documentation under the first example (http://docs.couchdb.org/en/1.6.1/couchapp/views/collation.html#views-collation), it is suggested that it is not recommended to emit the document itself in the view and instead, it is suggested to include the bodies of the documents when requesting the view, by requesting the view with ?include_docs=true.

如果我理解正确,而不是:

If I understood it correctly, instead of:

emit(doc._id, doc);

并以以下格式获取结果:

and getting results in the following format:

{"id":"1","key":"1","value":{"_id": "1", "someProp": "someVal"}},

建议发送带有空值的发射:

it is suggested to send emits with null values:

emit(doc._id, null)

然后使用include_docs参数查询我的视图时,将获得以下格式的结果:

and then when querying my view with the include_docs parameter get results in the following format:

{
    "id": "1",
    "key": "1",
    "value": null,
    "doc": {
        "_id": "1",
        "_rev": "1-0eee81fecb5aa4f51e285c621271ff02",
        "someProp": "someVal"
}

如果有建议的话,那我会认为它的性能会更好,但是不幸的是,文档没有详细说明为什么,而其他示例通常将文档作为值发出。有人可以对此进行更多说明吗?

If it is suggested, than I would presume the performance of that would be better, but unfortunately the documentation doesn't elaborate why and other examples emit documents normally as value in the emit. Could anyone shed more light on this?

推荐答案

当您发射时,在视图中查看整个文档,您可以有效地在磁盘上复制文档。这是因为每个视图都有自己的文件,其中包括在数据库上运行视图的结果。因此,如果在输出文档的位置有3个视图,则有4个副本左右浮动。 (不计算文档的多个修订版本,这当然会添加更多的副本)

When you emit the entire document in a view, you are effectively duplicating the document on disk. This is because each view has it's own file that includes the results of running the view on the database. Thus, if you have 3 views where you output your document, you have 4 copies floating around. (not counting multiple revisions of documents, which of course adds more duplicates)

CouchDB非常自由地使用磁盘空间以使写入速度更快,这主要是由于它们的选择使用仅附加结构。结果,使用视图重复输出相同的文档可能会导致磁盘使用量快速增长。 (压缩数据库和视图通常会有所帮助,但这不应该是您强迫自己不断进行的事情)。

CouchDB uses disk-space very liberally in order to make writes occur faster, largely due to their choice to use an append-only structure. As a result, using views to output the same document repeatedly can cause your disk-usage to grow very quickly. (compacting your database and views generally helps, but it should not be something you want to force yourself into constantly)

舍弃文档的权衡在于您从视图中读取时,CouchDB将需要在内部查找文档并将其包含在视图的输出中。由于它是根据ID查找信息,因此操作非常快捷,但仍会产生开销。因此,尽管这种模式通常是最佳实践,但您应该对在应用程序上下文中的权衡取舍持开放态度。

The trade-off to leaving the documents out is that when you are reading from the view, CouchDB will need to internally find the document and include it in the view's output. Since it is looking things up based on the id, it's a very fast operation, but it still incurs overhead. Thus, while this pattern is generally best-practice, you should be open to examining the trade-off in the context of your application.

这篇关于为什么CouchDB文档建议不应在视图中发出文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆