如何避免多个查询有:Rails中包括哪些内容? [英] How do I avoid multiple queries with :include in Rails?

查看:126
本文介绍了如何避免多个查询有:Rails中包括哪些内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我这样做

post = Post.find_by_id(post_id, :include => :comments)

两个查询被执行(一个用于发布数据,并和另一个用于这篇文章的评论)。然后,当我做post.comments,不进行其他查询,因为数据已经被缓存。

two queries are performed (one for post data and and another for the post's comments). Then when I do post.comments, another query is not performed because data is already cached.

有没有办法做的只是一个查询,仍然可以通过post.comments访问评论?

Is there a way to do just one query and still access the comments via post.comments?

推荐答案

没有,没有。这是预期的行为:包括,因为加入办法最终都证明是低效的。

No, there is not. This is the intended behavior of :include, since the JOIN approach ultimately comes out to be inefficient.

例如,请考虑以下情形:在发布模型,您需要选择3场,2场为注释,而这个特殊的职位有100个评论。导轨的可以的运行单个加入线沿线的查询:

For example, consider the following scenario: the Post model has 3 fields that you need to select, 2 fields for Comment, and this particular post has 100 comments. Rails could run a single JOIN query along the lines of:

SELECT post.id, post.title, post.author_id, comment.id, comment.body
FROM posts
INNER JOIN comments ON comment.post_id = post.id
WHERE post.id = 1

这将返回结果如下表:

 post.id | post.title | post.author_id | comment.id | comment.body
---------+------------+----------------+------------+--------------
       1 | Hello!     |              1 |          1 | First!
       1 | Hello!     |              1 |          2 | Second!
       1 | Hello!     |              1 |          3 | Third!
       1 | Hello!     |              1 |          4 | Fourth!
...96 more...

您可以看到这个问题了。单查询加入的办法,但它返回你所需要的数据,冗余返回。当数据库服务器发送结果集到Rails的,它会发出帖子的ID,标题和作者ID各100次。现在,假设发表过10场,你有兴趣,其中8人文本块。 EWW。这是一个很大的数据。将数据从数据库中的Rails的确实的把工作带两侧,无论是在CPU周期和内存,因此尽量减少数据传输是为了使应用程序的运行速度和重要的精简。

You can see the problem already. The single-query JOIN approach, though it returns the data you need, returns it redundantly. When the database server sends the result set to Rails, it will send the post's ID, title, and author ID 100 times each. Now, suppose that the Post had 10 fields you were interested in, 8 of which were text blocks. Eww. That's a lot of data. Transferring data from the database to Rails does take work on both sides, both in CPU cycles and RAM, so minimizing that data transfer is important for making the app run faster and leaner.

Rails的开发者嘎吱嘎吱数字,和大多数应用运行得更好使用多​​个查询只读取数据的每一位一次,而不是一个查询具有潜力时获得巨大冗余

The Rails devs crunched the numbers, and most applications run better when using multiple queries that only fetch each bit of data once rather than one query that has the potential to get hugely redundant.

当然,总会有一段时间在每个开发人员的生活,当一个连接是必要的,以便运行复杂的条件,并可以通过更换来实现:包括:加入。对于prefetching关系,但是,这种方法的Rails需要在:包括是多获得更好的性能。

Of course, there comes a time in every developer's life when a join is necessary in order to run complex conditions, and that can be achieved by replacing :include with :joins. For prefetching relationships, however, the approach Rails takes in :include is much better for performance.

这篇关于如何避免多个查询有:Rails中包括哪些内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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