您如何热切限制加载?

编程入门 行业动态 更新时间:2024-10-28 18:25:53
本文介绍了您如何热切限制加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在急切加载的文档中指出:

In the documentation for eager loading it is stated that:

如果您希望使用指定的:limit选项加载关联,它将被忽略,并返回所有关联的对象:

class Picture < ActiveRecord::Base has_many :most_recent_comments, :class_name => 'Comment', :order => 'id DESC', :limit => 10 end

Picture.find(:first,:include =>:most_recent_comments).most_recent_comments#=>返回所有关联的注释.

Picture.find(:first, :include => :most_recent_comments).most_recent_comments # => returns all associated comments.

如果是这种情况,那么达到加载极限"的最佳方法是什么?

If this is the case then what is the best way to achieve the "limit" on the loading?

比方说,我们渴望将最后10篇博客文章加载到博客的首页上,我们显然不希望全部都这样,那么应该指定帖子集合的限制和顺序吗?

Let's say we're eager loading the last 10 blog posts onto the front page of a blog, we clearly don't want them all so should the limit and ordering of the post collection be specified?

此外,是否可以在深度加载的元素上指定相同的条件-例如,仅在每篇博客文章中显示前三个评论?

Further to that, can one specify the same conditions on elements that are deep loaded - for instance only show the first three comments on each blog post?

Blog.find(:blog_id, :include => {:posts => :comments } )

推荐答案

我相信这是因为sql中的LIMIT命令不能很好地转换为您要尝试执行的操作. LIMIT将限制查询返回的总行数.您虽然没有尝试这样做.您正在尝试限制返回的每张图片的连接行数.为了达到这种效果,您必须使用一些复杂的SQL,如果表很大,可能很难对其进行优化.到那时,我会考虑为什么您要限制渴望加载的行.

I believe this is because the LIMIT command in sql doesn't translate well to what you are trying to do. LIMIT will limit the total rows returned by the query. You aren't trying to do that though. You are trying to limit the number of rows joined for each picture returned. In order to achieve this affect you'd have to use some complex SQL, which might be hard to optimize if your tables are large. At that point I would consider why you are trying to limit the rows eager loaded.

如果渴望加载的注释的最大数量是可管理的(<2000左右),则您可能不必担心在SQL端进行限制.

If the max number of comments eager loaded is manageable (< 2000 or so), you should probably not worry about limiting on the SQL end.

但是,如果您仅加载10个帖子,那么我将考虑根本不急于加载.我不希望额外的10个查询会使速度变慢,并且它们添加的时间是多少,您可以通过尝试其他优化技术(例如缓存)来弥补.

If you're only loading 10 posts though, I would consider not eager loading at all. I wouldn't expect an extra 10 queries to slow things down much, and what time they did add, you could make up for by trying other optimization techniques such as caching.

您应该让示波器为您完成肮脏的工作,而不是关联.这提高了可重用性,可维护性和可读性.示例:

You should let scopes do the dirty work for you, not the assocation. This promotes reusability, maintainability, readability. Example:

Class Picture < ActiveRecord::Base has_many :comments, :order => 'id DESC' do def recent limit(10) end end end

这样ments会在需要时出现,并且您还可以像这样缩小范围:

That way ments is there when you need it, and you can also scope it down like this:

@picturements.recent

更多推荐

您如何热切限制加载?

本文发布于:2023-10-20 03:13:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1509672.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:热切   加载

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!