Play框架中Morphia和MongoDB模块的性能差异是什么?
Play 框架中的 Morphia 和 MongodDB 模块都是 MongoDB 的 Java 驱动程序的包装器。
但我注意到 Morphia 在查询时并没有直接给出 Java 列表。它给了我一个名为 Query 的复杂对象。在这个对象上,我必须调用方法 asList() ,这会导致它迭代查询中的每个元素并生成一个列表。我认为当我大多数时候需要 Java 列表时,这会对性能产生影响。我想知道为什么 Morphia 在从 mongodb 数据库获取数据时不生成 List。
Both Morphia and MongodDB Module in play framework are wrappers around Java driver for MongoDB.
But I noticed Morphia does not give directly a Java List when querying. It gives me a complex object called Query. On this object I have to call method asList() which causes it to iterate over each element in Query and generate a List. I think this can have performance impact when most of the time I need Java lists. I wonder why Morphia does not generate List at the time it fetches data from mongodb database.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
原因是它可以让您决定如何获取数据。正如 wiki 中所示,您只能通过 get() 检索第一个对象,或通过 asKeyList() 获取 id 列表。 他们承认,对于大型集合来说,使用 as List() 的成本可能会很高。
据我了解,背后的原因是重用 Query 对象。它们允许您构建复杂的查询对象(带有过滤器等)并在需要时检索结果。您甚至可以从同一查询检索不同的结果集,因为列出的方法(asList 等)不会影响查询对象。
如果您将大量重用查询对象,并且不会返回大量数据(无论如何您不应该太高兴地这样做),那么这可能很有用。
The reason is that it lets you decide how you want your data. As seen in the wiki you could only retrieve the first object via get(), or a list of id via asKeyList(). They acknowledge that using as List() can be costly for large sets.
The reason behind, for what I understand, is reusing the Query object. They let you build a complex Query object (with filters and such) and retrieve the results when required. You could even retrieve different sets of results from the same Query, as the listed methods (asList, etc) don't impact the query object.
If you will reuse your query objects a lot, and you won't return huge sets of data (which you shouldn't do too happily anyway) this can be useful.