From 0bc9340102ffe17f29e81a3088f72ce59bc51f53 Mon Sep 17 00:00:00 2001 From: Magnus Hagander Date: Wed, 21 Oct 2020 20:57:47 +0200 Subject: [PATCH] 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. --- pgweb/docs/views.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/pgweb/docs/views.py b/pgweb/docs/views.py index ef196a17..a10664e1 100644 --- a/pgweb/docs/views.py +++ b/pgweb/docs/views.py @@ -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 # doc alias with a redirect, and if so, redirect to that page try: - page = DocPage.objects.get(version=ver, file=fullname) + page = DocPage.objects.select_related('version').get(version=ver, file=fullname) except DocPage.DoesNotExist: # 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 @@ -106,19 +106,16 @@ def docpage(request, version, filename): url = "/docs/{}/{}".format(version, page_redirect.redirect_to) 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)"], params=[fullname, fullname, fullname], - select={ - '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') + ).order_by('-version__supported', 'version').only('version', 'file') return render(request, 'docs/docspage.html', { 'page': page, - 'supported_versions': [v for v in versions if v.supported], - 'devel_versions': [v for v in versions if not v.supported and v.testing], - 'unsupported_versions': [v for v in versions if not v.supported and not v.testing], + 'supported_versions': [v for v in versions if v.version.supported], + '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.version.supported and not v.version.testing], 'title': page.title, 'doc_index_filename': indexname, 'loaddate': loaddate, -- 2.39.5