#11082: exclude subquery executed twice ---------------------------------------------------+------------------------ Reporter: handrews | Owner: nobody Status: new | Milestone: 1.1 Component: Database layer (models, ORM) | Version: SVN Resolution: | Keywords: Stage: Accepted | Has_patch: 0 Needs_docs: 0 | Needs_tests: 0 Needs_better_patch: 0 | ---------------------------------------------------+------------------------ Comment (by clement):
Did a bit of digging, and it seems that this extra query is issued (gets in django.db.connection.queries) when the Q object for the exclude clause is inverted (then deepcopied). See [http://code.djangoproject.com/browser/django/trunk/django/db/models/query.py#L482 django.db.models.query] and [http://code.djangoproject.com/browser/django/trunk/django/db/models/query_utils.py#L151 django.db.models.query_utils]. Interestingly enough, as Q._combine uses also deepcopy, the same problem exists when using a subquery in a Q object being a left hand operand on an & or | operation. Example : {{{ >>> pub_all = Publisher.objects.all() >>> django.db.connection.queries [] >>> Book.objects.filter( Q(publisher__in=pub_all) & Q(name='aa') ) [] >>> django.db.connection.queries [{'time': '0.001', 'sql': u'SELECT `app_publisher`.`id`, `app_publisher`.`name` FROM `app_publisher`'}, {'time': '0.001', 'sql': u'SELECT `app_book`.`id`, `app_book`.`name`, `app_book`.`publisher_id` FROM `app_book` WHERE (`app_book`.`publisher_id` IN (SELECT U0.`id` FROM `app_publisher` U0) OR `app_book`.`name` = aa ) LIMIT 21'}] }}} -- Ticket URL: <http://code.djangoproject.com/ticket/11082#comment:3> Django <http://code.djangoproject.com/> The Web framework for perfectionists with deadlines. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/django-updates?hl=en -~----------~----~----~----~------~----~------~--~---