【问题标题】:Rails preserve only 1 out of many records per day. Keep last, Delete restRails 每天只保留许多记录中的 1 个。保留最后,删除其余部分
【发布时间】:2012-04-30 02:14:50
【问题描述】:

Rails 3.1、Ruby 1.9.2、AR/MySQL。

如果同一类型的结果在此期间有很多结果,我正在寻找有关如何在每个时间段(天)仅保留 1 个结果的建议。一个例子可能是跟踪股票价格。最初,我们将每 15 分钟保存一次价格,但只需要将每个价格点存储 1 周。第一周之后,我们每天只需要 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

提前感谢您的任何帮助/建议。我会在完成它的过程中尝试更新,希望它对以后的人有所帮助。

【问题讨论】:

    标签: ruby-on-rails ruby activerecord


    【解决方案1】:

    您可以通过在 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() 组合在一起。 group()update_all() 忽略。

    【讨论】:

      【解决方案2】:

      而不是对每个赞都调用 delete

       (v[0]..v[v.size-2]).each {|r| r.delete}
      

      删除所有但不是最后一个

      price_ids_to_keep = []
      if v.count > 1  # if many price points that day
        price_ids_to_keep << v[-1].id # get the last
      else
        price_ids_to_keep << v[0].id
      end
      
      prices.where('id not in (?)',price_ids_to_keep).delete_all
      

      我从来没有这样做过,但我很确定它应该可以工作


      这样会更好,因为它会减少 DELETE 查询,但应该有一种方法可以在一个大查询中完成所有这些


      从商业角度来看,您或您的团队应该更好地考虑这一点。现在的存储很便宜,这样的信息对于未来的数据挖掘和类似的东西来说可能很宝贵。

      【讨论】:

        猜你喜欢
        • 2019-03-30
        • 2012-04-12
        • 1970-01-01
        • 1970-01-01
        • 2013-06-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多