2

I have been working in Rails (I mean serious working) for last 1.5 years now. Coming from .Net background and database/OLAP development, there are many things I like about Rails but there are few things about it that just don't make sense to me. I just need some clarification for one such issue.

I have been working on an educational institute's admission process, which is just a small part of much bigger application. Now, for administrator, we needed to display list of all applied/enrolled students (which may range from 1000 to 10,000), and also give a way to export them as excel file. For now, we are just focusing on exporting in CSV format.

My questions are:

  1. Is Rails meant to display so many records at the same time?

  2. Is will_paginate only way to paginate records in Rails? From what I understand, it still fetches all the records from DB, and then selectively displays relevant records. Back in .Net/PHP/JSP, we used to create stored procedure and from there we selectively returns relevant records. Since, using stored procedure being a known issue in Rails, what other options do we have?

  3. Same issue with exporting this data. I benchmarked the process i.e. receiving request at the server, execution of the query and response return. The ActiveRecord creation was taking a helluva time. Why was that? There were only like 1000 records, and the page showed connection timeout at the user. I mean, if connection times-out while working on for 1000 records, then why use Rails or it means Rails are not meant for such applications. I have previously worked with TB's of data, and never had this issue.

  4. I never understood ORM techniques at the core. Say, we have a table users, and are associated with multiple other tables, but for displaying records, we need data from only tables users and its associated table admissions, then does it actually create objects for all its associated tables. I know, the data will be fetched only if we use the association, but does it create all the objects before-hand?

I hope, these questions are not independent and do qualify as per the guidelines of SF.

Thank you.

EDIT: Any help? I re-checked and benchmarked again, for 1000 records, where in we are joining 4-5 different tables (1000 users, 2-3 one-to-one association, and 2-3 one-to-many associations), it is creating more than 15000 objects. This is for eager loading. As for lazy loading, it will be 1000 user query plus some 20+ queries). What are other possible options for such problems and applications? I know, I am kinda bumping the question to come to top again!

4

1 回答 1

0

Rails 可以处理具有 TB 数据的数据库。

will_paginate 是在 Rails 中对记录进行分页的唯一方法吗?

还有许多其他宝石,例如“kaminari”。

它从数据库中获取所有记录..

不。它不是那样工作的。例如,采取以下查询,Users.all.page(1).per(10)

User.all 不会触发数据库查询,它会返回一个代理对象。然后在代理(ActiveRecord::Relation)上调用 page(1) 和 per(10)。当您尝试从代理对象访问数据时,它将执行数据库查询。活动记录将累积您传递的所有条件和参数,并在需要时执行 sql 查询。

进入rails控制台并输入u= User.all; "f";(第二条语句:“f”,是为了防止rails控制台调用代理上的to_s来显示结果。)

它不会触发任何查询。现在尝试u[0],它会触发一个查询。

ActiveRecord 的创建非常耗时

1000 条记录应该不会花费太多时间。

  • 检查从 db 触发的 sql 查询的数量。寻找 n+1 问题的迹象并通过预先加载来修复它们。
  • 检查任何 cpu 或内存密集型操作的记录序列化为 csv 格式。
  • 使用分析器并跟踪大部分时间消耗的函数。
于 2013-04-15T06:08:46.267 回答