什么是 MongoDB 中的游标? [英] What is a Cursor in MongoDB?

查看:17
本文介绍了什么是 MongoDB 中的游标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们对某些 Morphia 查询cursor not found exceptions感到困扰"noreferrer">asList 并且我发现了一个 关于 SO 的提示,这可能会非常消耗内存.

We are troubled by eventually occurring cursor not found exceptions for some Morphia Queries asList and I've found a hint on SO, that this might be quite memory consumptive.

现在我想了解更多有关背景的信息:有人可以解释一下(用英语),Cursor(在 MongoDB 中)实际上是什么?为什么会一直打开或者找不到?

Now I'd like to know a bit more about the background: can sombody explain (in English), what a Cursor (in MongoDB) actually is? Why can it kept open or be not found?

文档将游标定义为:

指向查询结果集的指针.客户端可以遍历游标以检索结果.默认情况下,游标在 10 分钟不活动后超时

A pointer to the result set of a query. Clients can iterate through a cursor to retrieve results. By default, cursors timeout after 10 minutes of inactivity

但这不是很有说服力.也许为查询结果定义一个 batch 可能会有所帮助,因为 文档还指出:

But this is not very telling. Maybe it could be helpful to define a batch for query results, because the documentation also states:

MongoDB 服务器批量返回查询结果.批处理大小不会超过最大 BSON 文档大小.对于大多数查询,第一批返回 101 个文档或刚好足以超过 1 兆字节的文档.后续批次大小为 4 兆字节.[...] 对于包含没有索引的排序操作的查询,服务器必须在内存中加载所有文档以在返回任何结果之前执行排序.

The MongoDB server returns the query results in batches. Batch size will not exceed the maximum BSON document size. For most queries, the first batch returns 101 documents or just enough documents to exceed 1 megabyte. Subsequent batch size is 4 megabytes. [...] For queries that include a sort operation without an index, the server must load all the documents in memory to perform the sort before returning any results.

注意:在我们有问题的查询中,我们根本不使用排序语句,但也不使用 limitoffset.

Note: in our queries in question we don't use sort statements at all, but also no limit and offset.

推荐答案

我绝不是 mongodb 专家,但我只是想补充一些去年在中型 mongo 系统中工作的观察结果.还要感谢 @xameeramir 关于游标一般如何工作的出色演练.

I am by no mean a mongodb expert but I just want to add some observations from working in a medium sized mongo system for the last year. Also thanks to @xameeramir for the excellent walkthough about how cursors work in general.

光标丢失"异常的原因可能有多种.我注意到的一个在这个答案中有解释.

The causes of a "cursor lost" exception may be several. One that I have noticed is explained in this answer.

游标位于服务器端.它不分布在副本集上,而是存在于创建时作为主实例的实例上.这意味着如果另一个实例作为主实例接管,则光标将丢失给客户端.如果旧的主节点仍然存在,它可能仍然存在但没有用.我想它会在一段时间后被垃圾收集起来.因此,如果您的 mongo 副本集不稳定或者您前面的网络不稳定,那么在进行任何长时间运行的查询时,您就不走运了.

The cursor lives server side. It is not distributed over a replica set but exists on the instance that is primary at the time of creation. This means that if another instance takes over as primary the cursor will be lost to the client. If the old primary is still up and around it may still be there but for no use. I guess it is garbaged collected away after a while. So if your mongo replica set is unstable or you have a shaky network in front of it you are out of luck when doing any long running queries.

如果游标想要返回的全部内容不适合服务器的内存,则查询可能会很慢.您服务器上的 RAM 需要大于您运行的最大查询.

If the full content of what the cursor wants to return does not fit in memory on the server the query may be very slow. RAM on your servers needs to be larger than the largest query you run.

所有这些都可以通过更好的设计来部分避免.对于需要长时间运行的大型查询的用例,您最好使用几个较小的数据库集合,而不是一个大的数据库集合.

All this can partly be avoided by designing better. For a use case with large long running queries you may be better of with several smaller database collections instead of a big one.

这篇关于什么是 MongoDB 中的游标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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