Saturday, June 8, 2013

Re: Django foreing keys joins

Thanks!,

that query is making the join, and i wan't tho make a faster query, Joins ar not slow but if i can avoid it better.
>>> print Counter.on_medicos.filter(loc_id__isnull=True).only('id').query

My solution was: instead of  put null values on empty relations, put a 0 value.. It work's very good.

Thank for your answer, 



2013/6/6 Jani Tiainen <redetin@gmail.com>
That is how Django works.

So as you see, django makes query where it compares _joined_ table id as a null. To make it happen (left outer) join must happen, otherwise it wouldn't return any rows.

I agree that Django _could_ do a better and optimize query so that it would query instead of underlying fk field as a null value, though that would not be simple since a you know, filters may be chained and in case of chaining rewriting whole query would require quite a bit of reworkings.

If you absolutely know that your query stops there you can use underlying id field for faster testing:

>>> print Counter.on_medicos.filter(loc_id__isnull=True).only('id').query

That should return what you might have been expecting.

--

Jani Tiainen

"Impossible just takes a little longer"

On Thu, 6 Jun 2013 01:05:32 +0200
Àlex Pérez <alex.perez@bebabum.com> wrote:

> Can someone tell me why the or in that query:
> print Counter.on_medicos.filter(loc__isnull=True).only("id").query
>
> ""SELECT `web_counter`.`id` FROM `web_counter` LEFT OUTER JOIN
> `locations_localidad` ON (`web_counter`.`loc` = `locations_localidad`.`id`)
> WHERE (`web_counter`.`mode` = 1  AND `locations_localidad`.`id` IS NULL)"""
>
> The orm is making a JOIN?? i can't understant!
>
> The foreignkey loc is nullable and blank, i don't understant why making a
> filter the orm is performing the join and making an exclude the ORM is not
> performing the JOIN...
>
> print Counter.on_medicos.exclude(loc__isnull=True).only("id").query
> SELECT `web_counter`.`id` FROM `web_counter` WHERE (`web_counter`.`mode` =
> 1  AND NOT (`web_counter`.`loc` IS NULL))
>
> Thank's!
>
> --
> Alex Perez
> alex.perez@bebabum.com
>
>  *bebabum* be successful
>
> c/ Còrsega 301-303, Àtic 2
> 08008 Barcelona
> http://www.bebabum.com
> http://www.facebook.com/bebabum
> http://twitter.com/bebabum
>
> This message is intended exclusively for its addressee and may contain
> information that is confidential and protected by professional privilege.
> If you are not the intended recipient you are hereby notified that any
> dissemination, copy or disclosure of this communication is strictly
> prohibited by law.
>
> Este mensaje se dirige exclusivamente a su destinatario y puede contener
> información privilegiada o confidencial. Si no es vd. el destinatario
> indicado,
> queda notificado que la utilización, divulgación y/o copia sin autorización
> está prohibida en virtud de la legislación vigente.
>
> Le informamos que los datos personales que facilite/ha facilitado pasarán a
> formar parte de un fichero responsabilidad de bebabum, S.L. y que tiene
> por finalidad gestionar las relaciones con usted.
> Tiene derecho al acceso, rectificación cancelación y oposición en nuestra
> oficina ubicada en c/ Còrsega 301-303, Àtic 2 de Barcelona o a la dirección
> de e-mail lopd@bebabum.com
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

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





--
Alex Perez
alex.perez@bebabum.com
 
 bebabum be successful

c/ Còrsega 301-303, Àtic 2
08008 Barcelona
http://www.bebabum.com
http://www.facebook.com/bebabum
http://twitter.com/bebabum

This message is intended exclusively for its addressee and may contain
information that is confidential and protected by professional privilege. 
If you are not the intended recipient you are hereby notified that any 
dissemination, copy or disclosure of this communication is strictly prohibited by law.

Este mensaje se dirige exclusivamente a su destinatario y puede contener
información privilegiada o confidencial. Si no es vd. el destinatario indicado,
queda notificado que la utilización, divulgación y/o copia sin autorización 
está prohibida en virtud de la legislación vigente.

Le informamos que los datos personales que facilite/ha facilitado pasarán a
formar parte de un fichero responsabilidad de bebabum, S.L. y que tiene 
por finalidad gestionar las relaciones con usted. 
Tiene derecho al acceso, rectificación cancelación y oposición en nuestra
oficina ubicada en c/ Còrsega 301-303, Àtic 2 de Barcelona o a la dirección de e-mail lopd@bebabum.com

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

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate