Using Rails 4.2 and PostgreSQL 9.4.5. I have a transaction log table (XLog) which I need to read in reverse, starting from the last record and continuing to process until specific conditions occur. I am able to accomplish this, but it requires an "all" method and is not efficient. In my test system, the first record appears after .06 seconds and all subsequent queries are infinitesimal, less than .001 seconds. I've tried many other queries but they don't produce the desired result. I am using ActiveRecord but could use native SQL queries for this, of course.
This is the test code which focuses only on reading XLog backwards from the last record:
class EtlLastProcessed
def initialize(company: nil)
ActsAsTenant.with_tenant(company) do
count = 0
clock = Time.now
xlogs = XLog.all.order("created_at DESC")
xlogs.each do |xlog|
puts Time.now - clock
clock = Time.now
break if count > 1000
count += 1
puts "Xlog date is #{xlog.created_at.in_time_zone.to_date}"
end
puts "ETL Last Processed done."
end
end
end