`
ybffnst
  • 浏览: 13745 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Rails 大数据处理

阅读更多

If you want to do a large data query such as finding all the 10,000,000 users to send email to them, you should use batched finder to avoid eating too much memory.

Imagine you have a newsletter system which is very famous and has 10,000,000 users. Every Monday morning, the system will send emails to all of the users.

In General

You may find all the users, then send emails to them one by one

User.all.each do |user|
  NewsLetter.weekly_deliver(user)
end

It may work, but do you know there are 10,000,000 users? This code snippet will find all of the users and create a ruby object for each user row in table. The server is unhappy due to too much memory is eaten by your newsletter application.

Improvement

From rails 2.3, find_each and find_in_batches are available for batched finder. We can use them to improve the performance of the newsletter application.

User.find_each do |user|
  NewsLetter.weekly_deliver(user)
end

Using find_each, the application only finds 1,000 users once, yield them, then handle the next 1,000 users, until the last 1,000 users. That means the application will only load 1,000 user objects into memory each time, the server is happy now.

1,000 is the default batch size, if you think it is small/big to you, you can use the :batch_size option to change it.

find_in_batches is similar to find_each except that it yields the array of objects

User.find_in_batches(:batch_size => 5000) do |users|
  users.each { |user| NewsLetter.weekly_deliver(user) }
end

This method is very useful for large data query, saving your memory and time.

 

摘自:http://rails-bestpractices.com/posts/52-use-batched-finder-for-large-data-query

 

注: In find_in_batches loop, the specific Class only has a range of its instances(e.g. 1~1000), so item.related_lessons can not find the item(e.g. 1001) out of the range..

 

 

分享到:
评论

相关推荐

    Rails项目源代码

    MVC模式将应用程序分为三个主要部分:模型(Model)处理数据逻辑,视图(View)负责显示用户界面,控制器(Controller)协调模型和视图的交互。在这个图片分享项目中,模型可能包括`User`(用户)、`Image`(图片)...

    Rails 101 入门电子书

    - 保护敏感数据。 #### 十一、练习作业4-User可以加入、退出社团 - **目标**: - 用户能够加入或退出特定Group。 - **实现过程**: - 添加Join/Quit操作。 - 更新数据库结构。 - 实现逻辑处理。 #### 十二、...

    rails指南 中文版

    9. **ActiveJob**:Rails的后台任务处理框架,可以配合各种队列服务(如Resque、Sidekiq等)处理异步任务。 10. **Rails Console**:提供了一个交互式的命令行工具,用于检查和调试应用,执行Ruby代码,以及与...

    rails2-sample

    此外,Rails还内置了许多实用功能,如ActiveRecord(用于数据库交互)、Action View(用于页面渲染)和Action Controller(用于处理用户请求),这些都使得开发者能够快速构建出功能丰富的Web应用程序。 #### 2. ...

    使用Aptana+Rails开发Rails Web应用(中文)

    模型负责数据操作,视图用于展示用户界面,而控制器则作为两者之间的桥梁,处理用户请求并调用模型方法。在Aptana中,你可以直接在这些目录中编写相应的Ruby代码,IDE会提供代码补全和错误检查等功能。 例如,要在...

    component base rails applications

    - 探讨了组件化应用中的迁移(Migrations)管理,也就是如何在组件内处理数据表的迁移。 5. 组件类型: - 书中可能会提到不同类型的Rails引擎,比如Plain Engine、Full Engine和Mountable Engine,并解释它们之间...

    rails 项目起步示例

    模型负责业务逻辑和数据处理,视图负责用户界面展示,控制器则协调模型和视图之间的交互。 2. **bin**:存放可执行文件,如`rails`脚本,用于启动服务器、运行测试等。 3. **config**:配置文件的集合,包括数据库...

    中文版rails教程

    Rails以其“约定优于配置”(Convention over Configuration)和“Don't Repeat Yourself”(DRY,不要重复自己)的原则,极大地提高了开发效率和代码可读性,成为了Web2.0时代敏捷开发的首选工具。 在Ruby on ...

    好用的rails 2.0 Api 文档

    模型负责数据处理和业务逻辑,视图则呈现用户界面,而控制器作为两者之间的协调者,处理用户的请求并传递数据给视图。 **2. Active Record** 在Rails 2.0中,Active Record是ORM(对象关系映射)的一部分,它使得...

    railsAPI

    在Rails API中,模型(Model)代表应用程序的数据结构,它与数据库交互,处理数据验证和业务逻辑。视图(View)是用户看到和与之交互的部分,而控制器(Controller)作为模型和视图之间的协调者,处理用户请求并决定...

    rails api(文档)

    在开发过程中,了解和参考Rails API文档将极大地帮助你理解和利用这个强大的框架,以构建健壮、高效的API服务。同时,熟悉相关的Gem和工具,如 grape、rabl 或 jbuilder,可以帮助你进一步定制和扩展你的API功能。

    Ruby on Rails Guides v2 - Ruby on Rails 4.2.5

    - **用途**:可用于在保存或删除记录前执行额外的逻辑处理。 ### 总结 通过以上介绍,我们不仅了解了Rails的基础知识,还深入探讨了其核心组件——Active Record的相关概念和技术细节。这些内容对于初学者来说是...

    Rails相关电子书汇总

    8. **ActiveSupport**:包含许多实用的工具类和模块,如时间戳处理、字符串处理等,它们被广泛地应用于Rails应用中。 9. **测试驱动开发(TDD)和集成测试**:Rails鼓励开发者使用Test::Unit或Rspec进行测试,确保...

    rails敏捷开发的购物车系统

    模型(Model)负责处理业务逻辑和数据存储,视图(View)用于展示用户界面,而控制器(Controller)作为模型和视图之间的桥梁,处理用户输入并协调二者之间的交互。在购物车系统中,模型可能包括商品、购物车、订单...

    Ruby on Rails入门例子

    3. **Controller**:控制器处理HTTP请求,调用模型进行数据操作,并将结果传递给视图进行渲染。控制器是MVC架构中连接模型和视图的桥梁。 在"Ruby on Rails入门例子"中,我们可能会遇到以下关键概念: - **路由...

    The Rails 4 Way

    - **Rack**:Rack是Ruby Web应用的一个接口规范,Rails基于Rack实现了自己的请求处理流程。 - **ActionDispatch**:ActionDispatch是Rails中处理HTTP请求的核心模块,负责解析请求并将请求分发到合适的控制器方法。 ...

    Rails 101S

    - **深入实践CRUD功能**:详细讲解如何具体实现CRUD操作,包括数据验证、错误处理等高级特性。 - **MVC架构与RESTful概念**:介绍模型(Model)、视图(View)、控制器(Controller)三者之间的关系以及RESTful API的设计...

    rails2.3.8 && ruby1.8.7

    1. `sqlite3.dll` 和 `sqlite.dll`:这是SQLite数据库的动态链接库文件,SQLite是一个轻量级的嵌入式数据库,常用于Rails开发中的本地数据存储,特别是在开发阶段或者小型项目中。 2. `rubyinstaller-1.8.7-p249-rc2...

Global site tag (gtag.js) - Google Analytics