【发布时间】:2019-08-06 04:07:00
【问题描述】:
我有一个带有这样架构的 topic_followers 表
id, user_id, topic_id, creation_date, modified_date
我想从这个表中生成一个散列,其中 user_id 作为键,这个用户关注的 topic_id 数组作为值。目前,我正在尝试使用以下代码来实现:
topic_followers = TopicFollower.select("user_id, topic_id")
topic_follower_hash = {}
topic_followers.each do |topic_follower|
topic_follower_hash[topic_follower.user_id] = topic_follower_hash[topic_follower.user_id] || []
topic_follower_hash[topic_follower.user_id] << topic_follower.topic_id
end
问题是,这是一张大桌子,我怕它会毁了我的记忆。我用谷歌搜索了一下,一些文章建议使用 find_in_batches。不过,我认为它不符合我的需要,因为用户关注的某些主题可能不在当前批次中。想知道解决此类问题的推荐做法是什么?
【问题讨论】:
-
您使用的是 SQL 还是 NoSQL?
标签: ruby-on-rails activerecord scalability