Ruby on Rails 是否“has_many”?数组提供有关“需要知道”的数据。基础?

发布于 2024-09-02 19:09:38 字数 464 浏览 5 评论 0原文

比如说,在 Ruby on Rails 上,如果 Actor 模型对象是 Tom Hanks,并且“has_many”粉丝是 20,000 个 Fan 对象,则

actor.fans

给出一个包含 20,000 个元素的数组。也许这些元素没有预先填充值?否则,从数据库获取每个 Actor 对象可能会非常耗时。

那么这是基于“需要知道”的基础吗?

那么当我访问actor.fans[500]时它会拉取数据,而当我访问actor.fans[0]时它会拉取数据吗?如果它从每条记录跳转到另一条记录,那么它将无法通过顺序读取来优化性能,这在硬盘上可能会更快,因为这些记录可能位于附近的扇区/盘片层中 - 例如,如果程序接触2个随机元素,那么读取这2条记录会更快,但是如果它以随机顺序接触所有元素,那么以顺序方式读取所有记录,然后处理随机数据可能会更快元素。但是 RoR 如何知道我是只做几个随机元素还是随机做所有元素呢?

On Ruby on Rails, say, if the Actor model object is Tom Hanks, and the "has_many" fans is 20,000 Fan objects, then

actor.fans

gives an Array with 20,000 elements. Probably, the elements are not pre-populated with values? Otherwise, getting each Actor object from the DB can be extremely time consuming.

So it is on a "need to know" basis?

So does it pull data when I access actor.fans[500], and pull data when I access actor.fans[0]? If it jumps from each record to record, then it won't be able to optimize performance by doing sequential read, which can be faster on the hard disk because those records could be in the nearby sector / platter layer -- for example, if the program touches 2 random elements, then it will be faster just to read those 2 records, but what if it touches all elements in random order, then it may be faster just to read all records in a sequential way, and then process the random elements. But how will RoR know whether I am doing only a few random elements or all elements in random?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

憧憬巴黎街头的黎明 2024-09-09 19:09:38

如果您只使用其中 2 条记录,为什么要获取 50000 条记录?然后从数据库中仅获取这两个。如果你想列出粉丝,那么你可能会使用分页 - 即在查询中使用限制和偏移量,或一些分页宝石,如 will_paginate。

我看不出任何合乎逻辑的解释为什么你要走你尝试的路。解释一下真实情况,以便我们可以帮助您。

然而,有人认为您需要知道从数据库加载许多关联对象时 - 使用 :include 这样

Actor.all(:include => :fans)

会急切加载所有粉丝,因此只会有 2 个查询而不是 N+1,其中 N 是演员的数量

Why would you want to fetch 50000 records if you only use 2 of them? Then fetch only those two from DB. If you want to list the fans, then you will probably use pagination - i.e. use limit and offset in your query, or some pagination gem like will_paginate.

I see no logical explanation why should you go the way you try to. Explain a real situation so we could help you.

However there is one think you need to know wile loading many associated objects from DB - use :include like

Actor.all(:include => :fans)

this will eager-load all the fans so there will only be 2 queries instead of N+1, where N is a quantity of actors

冰雪梦之恋 2024-09-09 19:09:38

查看开发模式下服务器喷出的 SQL,它会告诉你正在加载多少条粉丝记录。在这种情况下,actor.fans 确实会导致它们全部被加载,这可能不是您想要的。

您有多种选择:

  • 按照 Tadas 的建议使用分页器;
  • 与粉丝表建立另一个关联,仅提取您感兴趣的粉丝表。这可以通过 has_many 语句上的条件来完成,例如
    has_many:粉丝,:条件=> “居住国家=‘英国’”
  • 指定完整的 SQL 以缩小使用 :finder_sql 选项返回的行数
  • 指定 :limit 选项将限制返回的行数。

一切都取决于你想做什么。

Look at the SQL which is spewed out by the server in development mode, and that will tell you how many fan records are being loaded. In this case actor.fans will indeed cause them all to be loaded, which is probably not what you want.

You have several options:

  • Use a paginator as suggested by Tadas;
  • Set up another association with the fans table that pulls in just the ones you're interested in. This can be done either with a conditions on the has_many statement, e.g.
    has_many :fans, :conditions => "country of residence = 'UK'"
  • Specifying the full SQL to narrow down the rows returned with the :finder_sql option
  • Specifying the :limit option which will, well, limit, the number of rows returned.

All depends on what you want to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文