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

推荐订阅源

量子位
S
Securelist
MyScale Blog
MyScale Blog
Jina AI
Jina AI
罗磊的独立博客
The Cloudflare Blog
美团技术团队
博客园 - 叶小钗
阮一峰的网络日志
阮一峰的网络日志
博客园 - 三生石上(FineUI控件)
月光博客
月光博客
雷峰网
雷峰网
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
大猫的无限游戏
大猫的无限游戏
博客园 - Franky
博客园 - 聂微东
Y
Y Combinator Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
T
Tailwind CSS Blog
Attack and Defense Labs
Attack and Defense Labs
博客园_首页
Latest news
Latest news
Apple Machine Learning Research
Apple Machine Learning Research
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The Hacker News
The Hacker News
G
GRAHAM CLULEY
Simon Willison's Weblog
Simon Willison's Weblog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Proofpoint News Feed
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
U
Unit 42
D
Docker
Webroot Blog
Webroot Blog
N
Netflix TechBlog - Medium
T
Tor Project blog
C
Cyber Attacks, Cyber Crime and Cyber Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
The Last Watchdog
The Last Watchdog
B
Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
Microsoft Azure Blog
Microsoft Azure Blog
Security Latest
Security Latest
V2EX - 技术
V2EX - 技术
N
News | PayPal Newsroom
Microsoft Security Blog
Microsoft Security Blog

博客园 - 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