`
smallwolf
  • 浏览: 19966 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
阅读更多

A criticism often leveraged against Ruby on Rails is that it’s slow. It’s true that the Ruby
language is slower at some things than other languages used in web development con-
texts. That just means that you as a developer need to be aware of where Ruby and
Rails can eat up valuable processor cycles. Then you can avoid those hot-spots by
choosing less processor-hungry alternatives to some of the great Rails sugar when it
doesn’t provide justifiable benefits.
Once cause of slowness I’ve found is in converting the results of a SQL query into
ActiveRecord objects. A row from an SQL query result set is just a set of key-value pairs;
in other words, a hash. ActiveRecord objects are great in that they come bundled up
with methods that let you traverse object relationships, and they also contain methods
you’ve written into the classes to facilitate custom behavior. But along with all the sugar
that ActiveRecord provides comes a heavy overhead of creating the object itself.
Very often, especially when you’re selecting records to display a web page, all you need
are the key-value pairs. A hash would suffice, and it turns out getting your results out
of the database as hashes is much faster—more than 50% faster than requesting
ActiveRecord objects.
Whereas a regular query that retrieves ActiveRecord objects looks like this:

    
MyObject.find(:all)


a query that returns hashes looks like this:

    
MyObject.connection.select_all("select * from my_objects")


True, you resort to SQL here, but in slow pages where you need to eke out that last bit
of render-time performance, the trade-off can be worth it. In a test of retrieving 40
thousand objects on a MacBook Pro 2.33 Ghz Intel Core 2 Duo, the ActiveRecord
approach took seven seconds of Ruby time, while the hash method took three.
The other caveat worth noting when replacing ActiveRecord queries with hash queries
is that objects and hashes are accessed differently. You access objects with dot notation:
my_object.foo versus my_object['foo']. But that’s exactly the problem taken care of by
our core hash extension! Using this extension, you can cherry-pick slow ActiveRecord
queries that aren’t using all the “extras” given to you by the ActiveRecord object itself,
and swap out the query to boost performance.

 

分享到:
评论

相关推荐

    hash_extend:宝石 hash_extend

    gem 'hash_extend' 然后执行: $ bundle 或者自己安装: $ gem install hash_extend 用法 Stealth_delete -- 不推荐使用 -> 使用除了 方法 'stealth_delete' 已弃用,将在 2.4 版中删除 - 使用 'except' 以适应...

    com.guo.android_extend android-extend1.0.6.zip

    implementation 'com.guo.android_extend:android-extend:1.0.6'失败,用这个替代就好了 implementation 'com.guo.android_extend:android-extend:1.0.6'失败,用这个替代就好了

    com.guo.android_extend:android-extend:1.0.6

    implementation 'com.guo.android_extend:android-extend:1.0.6'失败,用这个替代就好了

    解析UML用例图中include与extend的区别

    解析 UML 用例图中 include 与 extend 的区别 UML 用例图是软件开发过程中的一种重要工具,它能够帮助我们更好地理解和描述业务需求。在 UML 用例图中,有两种重要的关系:include 和 extend。这两种关系都是用来...

    计算graphicLayers的extend

    计算graphicLayers的extend

    原生js实现jquery $.extend方法

    原生js实现jquery $.extend方法 通过遍历对象属性来实现

    jquery.validate.extend.js

    jquery.validate.extend.js

    jQuery.extend 函数详解

    jQuery.extend 函数详解 Jquery的扩展方法extend是我们在写插件的过程中常用的方法,该方法有一些重载原型,在此,我们一起去了解了解。

    【1积分】android-extend-1.0.6.aar

    使用方式:把资源放到libs下,在gradle app 中添加 低版本:compile files('libs/android-extend-1.0.6.aar') 高版本:implementation files('libs/android-extend-1.0.6.aar')

    EXTEND基础

    EXTEND工程设计软件的基础教程 英文版本 适合初学者

    Extend

    NULL 博文链接:https://anniexuguoan.iteye.com/blog/835266

    android-widget-extend

    各种控件组件展示。 支持API10+ 水平滑动listView。 异步加载图片。 双指缩放,拖动。 项目链接:https://github.com/gqjjqg/android-widget-extend

    EXTEND物流仿真模块

    EXTEND是一个很简朴但实用经济、建模效率很高、扩展性很好的仿真软件,性价比和众多技术性能指标在主流仿真软件中比较突出,深受重视实效的企业欢迎。进入中国很短时间之内,已经被英特尔、ABB、佳能、宝钢 、诺基亚...

    foam-extend-4.1第三方库

    foam-extend-4.1的地方库,安装时将库放在指定文件夹,不需要下载,可以直接安装编译。 库文件来自于互联网,用于学习使用。

    UML用例图之泛化(generalization)、扩展(extend)和包含(include)关系

    用例的关系有泛化(generalization)、扩展(extend)和包含(include)。其中include和extend最易混淆。下面我们结合实例彻底理清三者的关系。基本概念用例图(UseCaseDiagram):用例图显示谁是相关的用户,用户希望系统...

    前端开源库-extend-error

    前端开源库-extend-error扩展错误,在node.js中轻松定义自定义错误类型

    jira-manual.pdf

    admins looking to extend JIRA should also consult the JIRA Space in Confluence. Here are some pointers to get you started: • Not yet downloaded JIRA? You can get it from the JIRA download page. • ...

    前端开源库-extend-promise

    前端开源库-extend-promise扩展一些常用方法

Global site tag (gtag.js) - Google Analytics