ruby-on-rails – Rails每天只保留许多记录中的1个.保持最后,删除休息

Rails 3.1,Ruby 1.9.2,AR / MySQL.

如果相同类型的结果在此期间有很多结果,我正在寻找关于如何每个时间段(日)仅保留1个结果的建议.一个例子可能是跟踪股票价格.最初,我们会每15分钟节省一次价格,但只需要存储每个价格点一周.在第一周之后,我们每天只需要1个价格(最后记录,收盘价).

这是一个简单的第一次尝试,但是非常低效:

# stock has many prices,price has one stock
# get all prices for single stock older than 1 week
prices = stock.prices.where("created_at < ? ",Time.Now-1.week)  
prices.group_by{ |price| price.created_at.to_date }.each do |k,v| # group by day
  if v.count > 1  # if many price points that day
    (v[0]..v[v.size-2]).each {|r| r.delete} # delete all but last record in day
  end
end

在此先感谢您的任何帮助/建议.我将尝试更新,因为我通过它,希望它将帮助有人下线.

解决方法

您可以通过在sql中完成所有操作,并通过将范围限制为上次运行它来提高效率.此外,如果添加一列以将较旧的日终条目标记为“已存档”,则会使查询更加简单.存档价格是您在一周后不会删除的价格.

rails generate migration add_archived_to_prices archived:boolean

迁移之前,请在created_at列上修改迁移到索引.

class AddArchivedToPrices < ActiveRecord::Migration
  def self.up
    add_column :prices,:archived,:boolean
    add_index :prices,:created_at
  end

  def self.down
    remove_index :prices,:created_at
    remove_column :prices,:archived
  end
end

工作流程将是这样的:

# Find the last entry for each day for each stock using sql (more efficient than finding these in Ruby)
keepers =
  Price.group('stock_id,DATE(created_at)').
        having('created_at = MAX(created_at)').
        select(:id).
        where('created_at > ?',last_run) # Keep track of the last run time to speed up subsequent runs

# Mark them as archived
Price.where('id IN (?)',keepers.map(&:id)).update_all(:archived => true)

# Delete everything but archived prices that are older than a week
Price.where('archived != ?',true).
      where('created_at < ?",Time.Now - 1.week).
      where('created_at > ?',last_run). # Keep track of the last run time to speed up subsequent runs
      delete_all

最后要注意的是,请确保不要将group()和update_all()组合在一起.使用update_all()忽略group().

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...