Reduce number of queries to render a docs page

One query for each version, supported or unsupported, now replaced with
a single query to get all of them.
This commit is contained in:
Magnus Hagander
2020-10-21 20:57:47 +02:00
parent 77bfa5cc87
commit 0bc9340102

View File

@ -97,7 +97,7 @@ def docpage(request, version, filename):
# try to get the page outright. If it's not found, check to see if it's a # try to get the page outright. If it's not found, check to see if it's a
# doc alias with a redirect, and if so, redirect to that page # doc alias with a redirect, and if so, redirect to that page
try: try:
page = DocPage.objects.get(version=ver, file=fullname) page = DocPage.objects.select_related('version').get(version=ver, file=fullname)
except DocPage.DoesNotExist: except DocPage.DoesNotExist:
# if the page does not exist but there is a special pgae redirect, check # if the page does not exist but there is a special pgae redirect, check
# for the existence of that. if that does not exist, then we're really # for the existence of that. if that does not exist, then we're really
@ -106,19 +106,16 @@ def docpage(request, version, filename):
url = "/docs/{}/{}".format(version, page_redirect.redirect_to) url = "/docs/{}/{}".format(version, page_redirect.redirect_to)
return HttpResponsePermanentRedirect(url) return HttpResponsePermanentRedirect(url)
versions = DocPage.objects.extra( versions = DocPage.objects.select_related('version').extra(
where=["file=%s OR file IN (SELECT file2 FROM docsalias WHERE file1=%s) OR file IN (SELECT file1 FROM docsalias WHERE file2=%s)"], where=["file=%s OR file IN (SELECT file2 FROM docsalias WHERE file1=%s) OR file IN (SELECT file1 FROM docsalias WHERE file2=%s)"],
params=[fullname, fullname, fullname], params=[fullname, fullname, fullname],
select={ ).order_by('-version__supported', 'version').only('version', 'file')
'supported': "COALESCE((SELECT supported FROM core_version v WHERE v.tree=version), 'f')",
'testing': "COALESCE((SELECT testing FROM core_version v WHERE v.tree=version),0)",
}).order_by('-supported', 'version').only('version', 'file')
return render(request, 'docs/docspage.html', { return render(request, 'docs/docspage.html', {
'page': page, 'page': page,
'supported_versions': [v for v in versions if v.supported], 'supported_versions': [v for v in versions if v.version.supported],
'devel_versions': [v for v in versions if not v.supported and v.testing], 'devel_versions': [v for v in versions if not v.version.supported and v.version.testing],
'unsupported_versions': [v for v in versions if not v.supported and not v.testing], 'unsupported_versions': [v for v in versions if not v.version.supported and not v.version.testing],
'title': page.title, 'title': page.title,
'doc_index_filename': indexname, 'doc_index_filename': indexname,
'loaddate': loaddate, 'loaddate': loaddate,