From efa535058baa968655bfdd4ccdd809d0a98e209f Mon Sep 17 00:00:00 2001 From: Paul Sturgess Date: Tue, 30 Apr 2024 15:56:52 +0100 Subject: [PATCH] perf: improve query performance for Metric.latest 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. --- lib/metricks/models/metric.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/metricks/models/metric.rb b/lib/metricks/models/metric.rb index 8f4da39..07c4bc8 100644 --- a/lib/metricks/models/metric.rb +++ b/lib/metricks/models/metric.rb @@ -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