【发布时间】:2015-04-19 02:37:40
【问题描述】:
我们已经在用户表上找到了一个“数据”列,其中有一个巨大的 json 转储。
现在,每次我们加载一堆用户时,我们都会将所有这些数据放入内存中,从而导致内存不足错误。
我们希望能够编写一些可以在我们的选择语句中使用的计算列。
eg:
Instead of doing this
user.data['profile_data']['data']['image']
We would like to add a column :image and then write a query like:
Here :name and :email are actual columns on the table and :image is a computed column:
Users.where(SOME_CONDITION).select(:name,:email,:image)
主要用例是我们显示所有用户的索引页面,它基本上为所有用户加载数据列
这样可以避免在内存中加载庞大的数据列,帮助我们从数据列中加载我们想要的字段
在 Rails4 中最好的方法是什么?
更新:
- 我们在 Heroku 上使用 postgres。
【问题讨论】:
-
这些都是
:name、:email、:image列来自data吗? -
不,他们不是。更新了问题“这里 :name 和 :email 是表上的实际列,而 :image 是计算列”
-
你用的是什么数据库?
标签: sql ruby-on-rails ruby ruby-on-rails-4 activerecord