Skip to content

Commit

Permalink
perf: improve query performance for Metric.latest
Browse files Browse the repository at this point in the history
Currently it pulls all records from the database but only uses the most recent amount value.

This is inefficient, particularly if the database is large. Now it only retrieves the amount from
the most recent record and ignores all the old ones.
  • Loading branch information
paulsturgess committed Apr 30, 2024
1 parent 62f2d56 commit efa5350
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/metricks/models/metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def record(type, **options)
# @return [Float]
def latest(type, **options)
scope = self.last(type, **options)
value = scope.pluck(:amount)&.first || 0.0
value = scope.select(:amount).first&.amount || 0.0
type.transform_amount(value, options[:associations])
end

Expand Down

0 comments on commit efa5350

Please sign in to comment.