Hi Cristiano,

If I get it correctly you'd like m2m querying to start with the 
intermediary (FROM) table and JOIN the referenced one only if more fields 
than the primary key are selected.

class Book(models.Model):
    name = models.CharField(max_length=100)

class Author(models.Model):
    books = models.ManyToMany(Book)

author = Author.objects.get(pk=1)
author.books.values_list('pk')

Would result in the following query:
SELECT book_id FROM author_books WHERE author_id = 1;

Instead of:
SELECT id FROM book JOIN author_books ON (book.id = author_books.book_id) 
WHERE author_id = 1;

I think this is a sensible optimization but I wonder about its feasibility. 
It looks like the `pk` reference would require some special handling to 
reference `book_id` since it's not actually a primary key on the 
intermediate table.

Simon

Le mercredi 18 novembre 2015 19:41:22 UTC-5, Cristiano Coelho a écrit :
>
> Hello there,
>
> Lets say I have these two models (sorry about the spanish names!) ( Django 
> 1.8.6 and MySQL backend )
>
> class Especialidad(models.Model):
>     nombre = models.CharField(max_length=250, blank=False, unique=True)
>
>
>
> class Usuario(AbstractBaseUser): 
>     permisosEspecialidad = models.ManyToManyField("Especialidad", blank=True)
>
> Let u be some Usuario instance, and the following query:
>
> u.permisosEspecialidad.all().values_list('pk',flat=True)
>
> The actual printed query is:
>
> SELECT `samiBackend_especialidad`.`id`
> FROM `samiBackend_especialidad` 
>       INNER JOIN `samiBackend_usuario_permisosEspecialidad` ON ( 
> `samiBackend_especialidad`.`id` = 
> `samiBackend_usuario_permisosEspecialidad`.`especialidad_id` ) 
> WHERE `samiBackend_usuario_permisosEspecialidad`.`usuario_id` = 8
>
> As my understanding, since I'm only selecting the id field which is already 
> present in the intermediary table (and is also a FK), the actual join is 
> redundant, as I have all the info I need in this case.
>
> So the query could work like this
>
> SELECT `samiBackend_usuario_permisosEspecialidad`.`especialidad_id`
> FROM  `samiBackend_usuario_permisosEspecialidad`
> WHERE `samiBackend_usuario_permisosEspecialidad`.`usuario_id` = 8
>
>
> I guess this works this way because this particular case might be hard to 
> detect or won't be compatible with any additional query building, however, 
> for ForeignKey relations, this optimization is already done (If you select 
> the primary key from the selected model only, it wont add a join)
>
> What would be the complications to implement this? Would it worth the effort?
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/03179775-ccca-41aa-9740-c174c28e1a37%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to