#25375: Paginator generates suboptimal queries
-------------------------------------+-------------------------------------
Reporter: alexei | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.7
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by alexei):
* status: closed => new
* cc: alexei (added)
* resolution: needsinfo =>
Comment:
Hello,
Sorry for the delay. I didn't get notified when you guys replied, and I
completely forgot about it.
Yes, in my previous example I was using MySQL.
Hoewever, I created a test app to see how it goes using the default
settings (including SQLite db). The result is as follows:
{{{
$ python manage.py shell
>>> from django.core.paginator import Paginator
>>> from django.db import connections
>>> from django.db.models import Count
>>> from bug.models import Article
>>> a = Article.objects.annotate(comments_count=Count('comments'))
>>> p = Paginator(a, 25)
>>> print p.object_list
[]
>>> print connections['default'].queries
[{u'time': u'0.001', u'sql': u'QUERY = u\'SELECT "bug_article"."id",
"bug_article"."title", "bug_article"."body", COUNT("bug_comment"."id") AS
"comments_count" FROM "bug_article" LEFT OUTER JOIN "bug_comment" ON (
"bug_article"."id" = "bug_comment"."article_id" ) GROUP BY
"bug_article"."id", "bug_article"."title", "bug_article"."body" LIMIT 21\'
- PARAMS = ()'}]
>>> print p.count
0
>>> print connections['default'].queries
[{u'time': u'0.001', u'sql': u'QUERY = u\'SELECT "bug_article"."id",
"bug_article"."title", "bug_article"."body", COUNT("bug_comment"."id") AS
"comments_count" FROM "bug_article" LEFT OUTER JOIN "bug_comment" ON (
"bug_article"."id" = "bug_comment"."article_id" ) GROUP BY
"bug_article"."id", "bug_article"."title", "bug_article"."body" LIMIT 21\'
- PARAMS = ()'}, {u'time': u'0.000', u'sql': u'QUERY = u\'SELECT COUNT(*)
FROM (SELECT "bug_article"."id" AS "id", "bug_article"."title" AS "title",
"bug_article"."body" AS "body", COUNT("bug_comment"."id") AS
"comments_count" FROM "bug_article" LEFT OUTER JOIN "bug_comment" ON (
"bug_article"."id" = "bug_comment"."article_id" ) GROUP BY
"bug_article"."id", "bug_article"."title", "bug_article"."body")
subquery\' - PARAMS = ()'}]
>>>
}}}
This time I'm again on Django 1.7.1.
For a second look, I updated Django to 1.8.4. The result is the same.
As you can see, it's Paginator's `count` that wraps the query resulting
from the QuerySet in a subquery.
--
Ticket URL: <https://code.djangoproject.com/ticket/25375#comment:5>
Django <https://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 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].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/064.f5f35f1bdad19dcd6c63abb05a9ba945%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.