Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
467e5700
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
467e5700
编写于
9月 10, 2014
作者:
R
Rafael Mendonça França
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #16379 from JackDanger/update-preloader-documentation
Updating Associations::Preloader docs
上级
44ed7a8e
27c9a23e
变更
1
隐藏空白更改
内联
并排
Showing
1 changed file
with
32 addition
and
23 deletion
+32
-23
activerecord/lib/active_record/associations/preloader.rb
activerecord/lib/active_record/associations/preloader.rb
+32
-23
未找到文件。
activerecord/lib/active_record/associations/preloader.rb
浏览文件 @
467e5700
...
@@ -2,33 +2,42 @@ module ActiveRecord
...
@@ -2,33 +2,42 @@ module ActiveRecord
module
Associations
module
Associations
# Implements the details of eager loading of Active Record associations.
# Implements the details of eager loading of Active Record associations.
#
#
# Note that 'eager loading' and 'preloading' are actually the same thing.
# Suppose that you have the following two Active Record models:
# However, there are two different eager loading strategies.
#
#
# The first one is by using table joins. This was only strategy available
# class Author < ActiveRecord::Base
# prior to Rails 2.1. Suppose that you have an Author model with columns
# # columns: name, age
# 'name' and 'age', and a Book model with columns 'name' and 'sales'. Using
# has_many :books
# this strategy, Active Record would try to retrieve all data for an author
# end
# and all of its books via a single query:
#
#
#
SELECT * FROM authors
#
class Book < ActiveRecord::Base
#
LEFT OUTER JOIN books ON authors.id = books.author_id
#
# columns: title, sales
#
WHERE authors.name = 'Ken Akamatsu'
#
end
#
#
# However, this could result in many rows that contain redundant data. After
# When you load an author with all associated books Active Record will make
# having received the first row, we already have enough data to instantiate
# multiple queries like this:
# the Author object. In all subsequent rows, only the data for the joined
#
# 'books' table is useful; the joined 'authors' data is just redundant, and
# Author.includes(:books).where(:name => ['bell hooks', 'Homer').to_a
# processing this redundant data takes memory and CPU time. The problem
#
# quickly becomes worse and worse as the level of eager loading increases
# => SELECT `authors`.* FROM `authors` WHERE `name` IN ('bell hooks', 'Homer')
# (i.e. if Active Record is to eager load the associations' associations as
# => SELECT `books`.* FROM `books` WHERE `author_id` IN (2, 5)
# well).
#
# Active Record saves the ids of the records from the first query to use in
# the second. Depending on the number of associations involved there can be
# arbitrarily many SQL queries made.
#
# However, if there is a WHERE clause that spans across tables Active
# Record will fall back to a slightly more resource-intensive single query:
#
# Author.includes(:books).where(books: {title: 'Illiad'}).to_a
# => SELECT `authors`.`id` AS t0_r0, `authors`.`name` AS t0_r1, `authors`.`age` AS t0_r2,
# `books`.`id` AS t1_r0, `books`.`title` AS t1_r1, `books`.`sales` AS t1_r2
# FROM `authors`
# LEFT OUTER JOIN `books` ON `authors`.`id` = `books`.`author_id`
# WHERE `books`.`title` = 'Illiad'
#
# This could result in many rows that contain redundant data and it performs poorly at scale
# and is therefore only used when necessary.
#
#
# The second strategy is to use multiple database queries, one for each
# level of association. Since Rails 2.1, this is the default strategy. In
# situations where a table join is necessary (e.g. when the +:conditions+
# option references an association's column), it will fallback to the table
# join strategy.
class
Preloader
#:nodoc:
class
Preloader
#:nodoc:
extend
ActiveSupport
::
Autoload
extend
ActiveSupport
::
Autoload
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录