Skip to content

Instantly share code, notes, and snippets.

@thoughtshop
Created November 16, 2013 16:01
Show Gist options
  • Save thoughtshop/7501856 to your computer and use it in GitHub Desktop.
Save thoughtshop/7501856 to your computer and use it in GitHub Desktop.
TransactionReport vs TransactionReport per StaffMember
if clumsy
staff_members = StaffMember.alphabetical
@reports = staff_members.map {|s| [ s, TransactionReport.where(:staff_member_id => s.id).where(:created_at => @date_range) ]}
end # 3000ms
if smart
report = TransactionReport.where(:created_at => @date_range)
@reports = report.group_by(&:staff_member).sort_by{|g| [g.first.last_name, g.first.first_name] }
end # 8000ms
@lcowell
Copy link

lcowell commented Nov 16, 2013

 TransactionReport Load (61.6ms)  SELECT "transaction_report".* FROM "transaction_report" WHERE ("transaction_report"."created_at" BETWEEN '2012-11-01' AND '2012-11-30')
  StaffMember Load (0.6ms)  SELECT "staff_members".* FROM "staff_members" WHERE "staff_members"."id" IN (20, 13, 6, 5, 26, 12, 18, 37, 73, 24, 52, 11, 31, 50, 32, 27, 53, 8, 4, 70, 71, 72, 28, 42, 16)
  Patient Load (4.4ms)  SELECT "patients".* FROM "patients" WHERE "patients"."id" IN (1910,......)
Benchmark.bm do |x|
  x.report("smart") do
    report = TransactionReport.where(:created_at => @date_range)
    @reports = report.group_by(&:staff_member).sort_by{|g| [g.first.last_name, g.first.first_name] }
  end
  x.report("clumsy") do
    staff_members = StaffMember.alphabetical
    @reports = staff_members.map {|s| [ s, TransactionReport.where(:staff_member_id => s.id).where(:created_at => @date_range).to_a ]}
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment