为什么这段代码比较慢?
只是将我的代码更新为 Rails 3 就绪,但是我遇到了以下代码的主要性能问题。
旧代码(又好又快)
@products = Product.all(
:order => 'name',
:include => [:category, :brand, :merchant]
).paginate(:page => params[:page])
新代码(慢 10 倍)
@products = Product.order("name")
.includes([:category, :brand, :merchant])
.paginate(:page => params[:page])
我添加了换行符以便于阅读。
我的数据库使用 Postgres,也许这就是问题所在?
任何其他使此代码变得更好的提示将不胜感激!
Just updating my code to be Rails 3 ready, however I'm experiencing a major performance issue with the code below.
Old code (nice and quick)
@products = Product.all(
:order => 'name',
:include => [:category, :brand, :merchant]
).paginate(:page => params[:page])
New code (10 x slower)
@products = Product.order("name")
.includes([:category, :brand, :merchant])
.paginate(:page => params[:page])
I added the line breaks for easy reading.
I'm using Postgres for my DB, maybe this is the issue?
Any other tips to make this code better will be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会检查输出的 SQL(从日志或控制台)以查看是否发生了更改和/或如何优化查询。
I'd check the outputted SQL (from logs or the console) to see if something's changed and/or how the query might be optimized.
会不会是分页的问题?您使用哪个插件进行分页?你可能想尝试Kaminari。
Can it be a problem with the pagination? Which plugin are you using for pagination? You might want to try Kaminari.