惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

美团技术团队
罗磊的独立博客
SecWiki News
SecWiki News
The Register - Security
The Register - Security
The GitHub Blog
The GitHub Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
S
Schneier on Security
IT之家
IT之家
博客园 - 聂微东
T
The Exploit Database - CXSecurity.com
Recorded Future
Recorded Future
大猫的无限游戏
大猫的无限游戏
Know Your Adversary
Know Your Adversary
Latest news
Latest news
Vercel News
Vercel News
G
GRAHAM CLULEY
D
DataBreaches.Net
D
Darknet – Hacking Tools, Hacker News & Cyber Security
S
SegmentFault 最新的问题
博客园_首页
雷峰网
雷峰网
T
Tenable Blog
Spread Privacy
Spread Privacy
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
酷 壳 – CoolShell
酷 壳 – CoolShell
Cisco Talos Blog
Cisco Talos Blog
V
Visual Studio Blog
J
Java Code Geeks
博客园 - Franky
The Cloudflare Blog
Apple Machine Learning Research
Apple Machine Learning Research
C
CERT Recently Published Vulnerability Notes
T
Threatpost
Google DeepMind News
Google DeepMind News
F
Fortinet All Blogs
P
Privacy International News Feed
T
Threat Research - Cisco Blogs
T
The Blog of Author Tim Ferriss
V
Vulnerabilities – Threatpost
Recent Announcements
Recent Announcements
Blog — PlanetScale
Blog — PlanetScale
Security Latest
Security Latest
U
Unit 42
M
MIT News - Artificial intelligence
Y
Y Combinator Blog
K
Kaspersky official blog
有赞技术团队
有赞技术团队
B
Blog
腾讯CDC

博客园 - Cure

Oracle 查看哪个表被锁定,并获取对应的sessionID Yii框架常见问题汇总 Yii框架中安装srbac扩展方法 Yii框架中使用PHPExcel导出Excel文件 Yii框架中使用SRBAC作为权限管理模块时遇到的问题 文件名过长,无法删除的解决办法 Yii框架中使用bootstrap SilverLight中显示上标,下标,平方 10 Websites To Download Free HTML/CSS Templates 经过两天的努力,终于写出了第一个android的helloword sqlplus 帮助无法显示问题的解决 COBOL中USAGE的用法 asp.net mvc开发项目的部署?? 初试asp.net mvc感受 临时搞两天VC,在VC里如何获取当前程序的名字和路径以及如何分割字符串 Borland以2300万美元卖掉CodeGear开发工具部门 Ruby on rails开发从头来(五十八)- ActiveRecord基础(自关联) 下载安装了ubuntu 8.04,感觉很好很强大 项目管理工具Redmine + SubVersion + Apache + windows环境安装搭建
Ruby on rails开发从头来(五十九)- ActiveRecord基础(预加载子记录)
Cure · 2008-06-04 · via 博客园 - Cure

    预加载子记录讨论的问题和“延迟加载”是相同的。通常Active Record会推迟从数据库中加载子记录,直到你需要他们,例如,通过Rdoc中的例子,我们假定博客程序有一个Model,像下面这样:

class Post < ActiveRecord::Base

belongs_to :author

has_many :comments, :order => 'created_on DESC'

end

如果我们遍历所有的post,访问作者和评论属性,我们使用一个Sql查询来返回posts表中的多条记录,并且对于每条post记录都要执行一次Sql来访问authors表和comments表,总共是2n+1次。

for post in Post.find(:all)

puts "Post: #{post.title}"

puts "Written by: #{post.author.name}"

puts "Last comment on: #{post.comments.first.created_on}"

end

上面的代码存在着严重的性能问题,我们可以通过find方法的:include参数来修正它,在find方法被执行时,会列出需要延迟加载的关联。Active Record会很聪明地使用一条SQL一次加载数据,如果有100post,和上面的代码相比,下面的代码将会消除100次数据库查询:

for post in Post.find(:all, :include => :author)

puts "Post: #{post.title}"

puts "Written by: #{post.author.name}"

puts "Last comment on: #{post.comments.first.created_on}"

end

这个例子还可以更简化,只使用一条SQL

for post in Post.find(:all, :include => [:author, :comments])

puts "Post: #{post.title}"

puts "Written by: #{post.author.name}"

puts "Last comment on: #{post.comments.first.created_on}"

end

预加载子记录并不能保证改善性能(事实上,如果你的数据库不支持左连接,那么你就不能使用延迟加载,如果你使用的是oracle8,就必须要升级到oracle9才可以),因为预加载在Sql中加入了所有的表,这样就会有很多记录被加载后转换成Model类的对象,这样就有可能导致内存使用的问题,特别是当一条记录有很多条子记录时,这种情况就更明显,与一条条地延迟加载子记录相比,预加载会占用更多的服务器内存。

如果你使用了:include,你需要使用find方法的参数消除列名的歧义,在列名前添加表名来区分,在下面的例子中,title列需要添加表名前缀:

for post in Post.find(:all, :conditions => "posts.title like '%ruby%'",

:include => [:author, :comment])

# ...

end