I'd try both and compare the processing time. What is the nature of
the Beta.name equivalent in your actual model? If it is a simple field
(e.g. INT) I doubt the overhead would be too bad. What might be
tripping me up is having your Gamma being linked to your Alpha through
an intermediate model. Since you mentioned Alpha is a payment model,
presumably Gamma is one of the constituent parts -- so why would there
not be a direct FK relationship between the two? (Or at least a one
way FK relationship through the intermediate model e.g. Beta->Gamma-
>Alpha.)

On May 5, 12:31 pm, Wedg <shak...@gmail.com> wrote:
> > qs = Alpha.objects.filter(gamma__beta__name='Beta').distinct())
> > id_list = [x.id for x in qs]
> > total =
> > Alpha.objects.filter(id__in=id_list).aggregate(total=models.Sum('id')).get( 
> > 'total')
>
> I'm sure this approach would work fine, however I feel like it might
> be a performance issue when the Alpha model (in my real world case, a
> Payment model) reaches a few thousand instances in that id_list. Maybe
> I'm wrong. If it were only a few dozen or even a few hundred, I would
> just do the processing in python and not worry about it. On the other
> hand, maybe python has some fancy optimizations for list comprehension
> that I'm not aware of, or DBs for processing large WHERE ... IN ...
> clauses.
>
> I guess I'll just start writing up the SQL for now and secretly hope
> that someone chimes in with a better solution.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to