Re: Queryset cloning is very expensive
Thank you for the reply. But I meant a slightly different case:
Item.objects.filter(...).order_by(...).values_list('id', flat=True)[:
10]
As you can see, there are four cloning operations. Wouldn't be
chaining possible without cloning the queryset? The method could just
return the original queryset. Seems like I'll have to write raw
queries in such cases :(
On Feb 11, 1:18 pm, kurvenschubser <malte.engelha...@gmx.net> wrote:
> Hi myx,
>
> cloning is useful for chaining of filters, e.g.
> User.objects.filter(name="Harry").exclude(lastname="Potter").filter(somethi ngelse="bla")
>
> But most of the time, I found I can construct querysets using a
> dictionary for collecting the query conditions:
>
> d = {name:"Harry", lastname:"Potter", somethingelse:"bla"}
> User.objects.filter(**d)
>
> For more complex queries, e.g.
>
> if not d:
> return
>
> q0 = Q(**dict(d.popitem()))
>
> for i, (k, v) in enumerate(d.items()):
> qx = Q(**{k:v})
> if i % 2:
> q0 | qx
> else:
> q0 & qx
>
> User.objects.filter(q0)
>
> On 10 Feb., 10:55, myx <i.virab...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Almost every method of QuerySet clones it. Sometimes it is very
> > ineficcient, for example when querysets are constructed in loops. I
> > have a function, which makes about 20 queries, and its execution time
> > is about 100ms. After profiling the function, I saw that clone() takes
> > most of the time: 70ms. It is called about 60 times, 1ms per call. The
> > queries themselves are very fast (about 15ms for all queries). So
> > constructing the query takes much longer time than executing it.
> > What cloning is needed for? Can I prevent queryset from cloning, or do
> > something else to reduce cpu consumption?
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home