News for hackers. Stuff that matters.
Re:It's not Ruby that's slow, it's your database by ColonelPhantom on Tuesday November 08, 2022 @10:23PM (#33527212)
In Django, you would write an annotation as documented at https://docs.djangoproject.com/en/4.1/topics/db/aggregation/...

So something like

  Album.objects.annotate(duration=Sum('track__length')).order_by('duration')
There are also libraries that enable you to define an annotation or aggregation inside a model, that you can then get with a call like select_properties, similar to the built-in select_related (which you use to get a foreign key in one query) and prefetch_related (which is select_related for many-to-many fields).
Re:It's not Ruby that's slow, it's your database by rahimnathwani on Tuesday November 08, 2022 @11:07PM (#33527418)
Thanks. I haven't used Django for ages, but do recall using annotate() in the past. At that time, I didn't need to worry about performance, so didn't look into what happens under the hood, i.e. whether the call generates more than one SQL query.