On Monday, 29 September 2014 23:15:36 UTC-4, Austin York wrote:
The idea is to provide additional project information if there are one or
more tasks on the project.
However, when invoked regularly on views this creates performance concerns
(especially with a growing data set).
What is the best way to optimize this query so that it doesn’t create N+1
query type issues when invoked from “each” loops in the view?
+1 what Michał said about eager-loading. One additional tricky thing:
prefer size over length for associations and relations. In plain
Ruby, size, length and count are more or less identical - but for
ActiveRecord collections they have slightly different meanings:
length is the most straightforward: it’s ALWAYS the number of
records
in the collection. If the collection isn’t currently loaded, calling length on it will trigger a SQL query to load all the records.
count is the opposite: it ALWAYS runs a SQL query. It doesn’t load
records, it uses SQL’s COUNT() function. It can also return things
that
aren’t numbers; doing Task.group(:project_id).count will give you back
a
hash with project_ids as keys and the number of matching tasks as
values.
size is the middle: if the collection is loaded, it works like length. Otherwise it works like count…