﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
23771	Optimisation idea for Paginator object	GP89	nobody	"I had a thought - I don't know if this can be easily achieved but here it is.

I have a view which uses the `Paginator` object to page results for a table, one of the columns of the tables is a total gathered from summing rows from 5 different tables, so the query has 5 joins to get these values.

  {{{#!python
run_objects = Run.objects.filter(type__name=run_type).select_related(""runend"", ""browserlink__browser"", ""filecheck__filecheckrecord"").\
    annotate(total_griderror=Count(""griderrorrecord"", distinct=True),
             total_gridhttperror=Count(""gridhttperrorrecord"", distinct=True),
             total_gridconnerror=Count(""gridconnectionerrorrecord"", distinct=True),
             total_storagehttperror=Count(""storagehttperrorrecord"", distinct=True),
             total_storageconnerror=Count(""storageconnectionerrorrecord"", distinct=True)).order_by(""-id"")

paginator = Paginator(runs_objects, 15)
  }}}

The `Paginator` object appears to do a Count query, to calculate the total number of pages and then a select query later when a page number is requested to get the data. The problem I'm seeing is that the count query in this case is including the 5 joins which don't affect the count other than making it a lot slower.

I was able to speed this up by performing the count for the `Paginator` myself

{{{#!python
run_objects = Run.objects.filter(type__name=run_type)
total = run_objects.count()

run_objects = run_objects.select_related(""runend"", ""browserlink__browser"", ""filecheck__filecheckrecord"").\
    annotate(total_griderror=Count(""griderrorrecord"", distinct=True),
             total_gridhttperror=Count(""gridhttperrorrecord"", distinct=True),
             total_gridconnerror=Count(""gridconnectionerrorrecord"", distinct=True),
             total_storagehttperror=Count(""storagehttperrorrecord"", distinct=True),
             total_storageconnerror=Count(""storageconnectionerrorrecord"", distinct=True)).order_by(""-id"")

paginator = Paginator(runs_objects, 15)
paginator._count = total
}}}

I was wondering - would it be possible to do this automatically. Is there a way to drop bits off the query for the count query (ie like annotations, select_related, prefetches which would have no effect on the number of rows returned) to speed up things up?"	Cleanup/optimization	new	Database layer (models, ORM)	1.6	Normal		paginator optimization performance	mail@…	Accepted	0	0	0	0	0	0
