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.
分享到:
相关推荐
gem 'hash_extend' 然后执行: $ bundle 或者自己安装: $ gem install hash_extend 用法 Stealth_delete -- 不推荐使用 -> 使用除了 方法 'stealth_delete' 已弃用,将在 2.4 版中删除 - 使用 'except' 以适应...
标题 "com.guo.android_extend:android-extend:1.0.6" 提示我们这是一个 Android 库项目,属于开发者郭某的扩展库,版本号为 1.0.6。通常,这种格式表示的是 Maven 或 Gradle 的依赖坐标,用于在构建 Android 项目时...
implementation 'com.guo.android_extend:android-extend:1.0.6'失败,用这个替代就好了 implementation 'com.guo.android_extend:android-extend:1.0.6'失败,用这个替代就好了
在JavaScript开发中,`underscore` 和 `jQuery` 都提供了扩展对象的功能,即 `_.extend()` 和 `$.extend()` 方法。这两个方法允许开发者合并一个或多个对象的属性到目标对象中,实现对象间的属性拷贝。本文将深入...
"cesium-extend"项目是针对Cesium的功能扩展包,旨在增强其原生能力,提供更多的实用功能,如白模(Wireframe)展示和避让(Occlusion)处理。下面将详细解释这两个关键特性以及如何在Cesium中应用它们。 1. **白模...
解析 UML 用例图中 include 与 extend 的区别 UML 用例图是软件开发过程中的一种重要工具,它能够帮助我们更好地理解和描述业务需求。在 UML 用例图中,有两种重要的关系:include 和 extend。这两种关系都是用来...
### jQuery.extend 函数详解 #### 一、概述 在JavaScript前端开发中,jQuery是一个非常流行的库,它简化了许多常见的操作,比如DOM操作、事件处理、AJAX交互等。`jQuery.extend`是jQuery提供的一个用于扩展jQuery...
标题“ Extend”可能指的是在编程领域中的扩展或继承概念,这在面向对象编程(OOP)中至关重要。在这个场景下,“ Extend”意味着一个类(子类)从另一个类(父类)继承属性和方法,从而实现代码重用和类的分层结构...
虹软ArcSoft 人脸识别AndroidDemo中有一个Lib通过公网始终无法下载,替换进去就好了。 依赖替换为 implementation files('libs/android-extend-1.0.5.aar')。 依赖库com.guo.android_extend
### EXTEND基础知识点详解 #### 一、EXTEND软件概览 **EXTEND**是一款由刃之砺信息科技(上海)有限公司开发的工程设计软件,主要用于动态系统建模与仿真。该软件具备强大的功能和先进的技术,适用于各种专业领域...
原生js实现jquery $.extend方法 通过遍历对象属性来实现
《深入理解foam-extend-4.1第三方库在OpenFOAM中的应用》 OpenFOAM,作为一款开源的计算流体动力学(CFD)软件,被广泛应用于工程和科研领域。它提供了丰富的功能和灵活性,使得用户可以对复杂的流体流动问题进行...
jquery.validate.extend.js
`$.fn.extend`是jQuery库中的一个核心方法,主要用于扩展jQuery对象的方法集合。这个方法允许开发者自定义jQuery的函数,从而实现对DOM元素的操作或添加新的功能。在jQuery中,`$.fn`实际上是`$.prototype`的一个...
Java 中 extend 与 implement 的区别 Java 语言中,extend 和 implement 是两个基本概念,它们之间的区别是非常重要的。extend 用于继承类,而 implement 用于实现接口。在 Java 中,不支持多重继承,但是可以使用...
使用方式:把资源放到libs下,在gradle app 中添加 低版本:compile files('libs/android-extend-1.0.6.aar') 高版本:implementation files('libs/android-extend-1.0.6.aar')
在前端开发中,错误处理是不可或缺的一部分,而`extend-error`正是一个专为前端和Node.js环境设计的开源库,用于扩展和定义自定义错误类型。这个库使得开发者能够更方便、更规范地管理和报告错误,提高代码的可读性...