- 浏览: 145944 次
- 性别:
- 来自: 北京
文章分类
最新评论
http://rails-bestpractices.com/posts/29-fix-n-1-queries
N+1 Queries is a serious database performance problem. Be careful of that situation! If you're not sure, I recommend you install http://github.com/flyerhzm/bullet plugin, which helps you reduce the number of queries with alerts (and growl).
Sample Model code
When @users.each in view, it cause N+1 queries. (N is 20 in this case)
Refactor
Add :include into query, so the number of queries reduces to only 2.
总结: 某种时候include 是必须的
N+1 Queries is a serious database performance problem. Be careful of that situation! If you're not sure, I recommend you install http://github.com/flyerhzm/bullet plugin, which helps you reduce the number of queries with alerts (and growl).
Sample Model code
# model class User < ActieRecord::Base has_one :car end - class Car < ActiveRecord::Base belongs_to :user end Bad Smell # your controller def index @users = User.paginate( :page => params[:page], :per_page => 20 ) end # view <% @users.each do |user| %> <%= user.car.name %> <% end %>
When @users.each in view, it cause N+1 queries. (N is 20 in this case)
Refactor
# your controller def index @users = User.paginate( :include => :car, :page => params[:page], :per_page => 20 ) end
Add :include into query, so the number of queries reduces to only 2.
总结: 某种时候include 是必须的
发表评论
-
Google App Engine and others
2010-09-15 11:27 765有用的技术文章 先记载 有空 研究下 さくっとRails ... -
基于jquery和mini_magick的图片裁剪
2010-09-15 11:07 1430引用网址 http://duyouhua1214.iteye ... -
ruby gem相关命令使用
2010-08-26 12:45 1558gem相关命令使用 1.显示gem的帮助和版本 gem – ... -
深入理解alias, alias_method和alias_method_chain
2010-08-06 17:14 949http://blackanger.blog.51cto.co ... -
Rails 3 特点
2010-08-06 16:38 868真的想留下来 http://railslove.com/w ... -
query-reviewer sql explain and review in the page
2010-08-06 12:20 1296引用网址 http://github.com/dsboulde ... -
request-log-analyzer log分析工具
2010-08-06 12:03 1573参考网址: http://github.com/wvanber ... -
IE7 Bug导致Rails Session保存失败
2010-08-06 10:56 950http://www.letrails.cn/archives ... -
Rails保存记录而不更新时间戳
2010-08-06 10:54 1323引用地址: http://www.letrails.cn/a ... -
how to generate qrcode on ror
2010-02-04 16:27 8501. In your rails project, in ... -
rmagick ror 上 安装
2009-11-18 09:06 828rmgick 安装规范 ror很多人现在在用了,但是发现 ... -
Update Rails to 2.2.2 (Rails 2.2.2)
2009-03-08 14:53 958gem install -v=2.2.2 rails Suc ... -
Use helpers in controllers or models
2009-01-19 16:22 889引用http://snipplr.com/view/2505/ ... -
open-flash-chart
2008-11-07 14:07 2401http://pullmonkey.com/2008/7/23 ... -
rake db:migrate
2008-11-07 11:58 1250【ruby on rails】rake db:migrate ... -
rss maker
2008-11-07 11:43 1080http://gdgdlog.net/log/show/130 ... -
acts_as_authenticated
2008-11-07 11:35 1062ruby script/plugin install act ... -
validates
2008-11-07 11:24 1037先週ずっとインフルエンザでダウンしてました。 今日はvail ... -
rails で model の変更を監視するプラグイン
2008-11-07 11:12 1346acts_as_modified モデルの変更を監視して、各 ... -
exception_notification
2008-11-07 11:03 11551 applicattion.rb 配置 incl ...
相关推荐
DevExpressVCL+17.2.4+V1+XE-RAD10.2.2+FullSource+Fix+By+Flying+Wang+自动编译安装+带汉化包+V2018-02-14.zip
从DELPHI2009一直到XE10.3所有版本的二次运行报错补丁+FASTDCC
mybatis的分页插件pagehelper-fix+文档
FastReport 5.6.1,支持D7-XE 10.2,FastReport 5.6.1,支持D7-XE 10.2
office卸载工具
自用
快速解决 建筑和安装 Full instructions: ...Quick instructions: ./bootstrap ./configure make make check sudo make install 对于SunOS和AIX,请查看README.SunOS和README.AIX。... 可以使用相对较新版本的cmake...
Microsoft Fix it,系统修复工具。
FastReports+5.2.12 FS
Main 97d Fix Cam 3d+otros
Fix the word-completion hanging problem by typing Chinese in ANSI document under Chinese Windows 7/8 64 bits. Enable word-completion in ANSI document under CJK environment. Add wildcard capacity ...
我从网上下载了一个GA-PSO算法,试着利用GA和PSO组合...我下载原始算法,有一个问题就是它是针对所有的设计变量上下限都是一样的,所以我对程序进行了修改与改进,现在可以处理上下限不一致的问题,同时fix了一些bug。
HP电子书制作软件 v3.2 Final + Fix3 补丁
canflashtool
Fix memory leak problem while switching tab. Fix User Defined Language dialog docking problem under Windows 7. Fix backwards search with Asian codepage problem. Add a new capacity in context menu: the...
微软GDI+图片漏洞360专用补丁包360gdi_fix.exe 微软GDI+图片漏洞360专用补丁包360gdi_fix.exe
FixUpdate与Update的区别 在 Unity 开发中, FixUpdate 和 Update 是两个常用的函数,但它们之间存在着重要的区别。今天,我们将深入探讨 FixUpdate 和 Update 的不同之处,并了解何时使用它们。 首先,让我们从 ...
lint-fix-nodemon 观看文件并在保存时运行ESLint + fix + nodemon 为什么需要这个? eslint修复文件时避免两次重新启动(nodemon通常会先检测到初始更改,然后再检测到修复更改) 如果挂起或启动服务器失败,请...
MELHORA PING FIX LATENCY
FixPlusPlus ____ ______ __ __ __ __ /\ _`\ /\__ _\/\ \ /\ \ /\ \ /\ \ \ \ \L\_\/_/\ \/\ `\`\/'/' \_\ \___ \_\ \___ \ \ _\/ \ \ \ `\/ > < /\___ __\/\___ __\ \ \ \/ \_\ \__ \/'/\`\/__/\ \_/\/__/\ \_/...