Use prefetch_related to reduce the number of queries on the search page and in idindex generation, adjust a couple of members on Document slightly to not filter on relations (filtering doesn't work with prefetch_related)

- Legacy-Id: 6992
This commit is contained in:
Ole Laursen 2013-12-18 16:58:34 +00:00
parent a656cf8a8b
commit defb116721
3 changed files with 9 additions and 7 deletions

View file

@ -122,7 +122,7 @@ class DocumentInfo(models.Model):
if not hasattr(self, "state_cache") or self.state_cache == None:
self.state_cache = {}
for s in self.states.all().select_related("type"):
for s in self.states.all():
self.state_cache[s.type_id] = s
return self.state_cache.get(state_type, None)
@ -357,7 +357,7 @@ class Document(DocumentInfo):
iesg_state = self.get_state("draft-iesg")
iesg_state_summary = None
if iesg_state:
iesg_substate = self.tags.filter(slug__in=IESG_SUBSTATE_TAGS)
iesg_substate = [t for t in self.tags.all() if t.slug in IESG_SUBSTATE_TAGS]
# There really shouldn't be more than one tag in iesg_substate, but this will do something sort-of-sensible if there is
iesg_state_summary = iesg_state.name
if iesg_substate:

View file

@ -112,9 +112,7 @@ def wrap_value(v):
def fill_in_search_attributes(docs):
# fill in some attributes for the search results to save some
# hairy template code and avoid repeated SQL queries - remaining
# queries we don't handle here are mostly implicit many-to-many
# relations for which there is poor support in Django 1.2
# hairy template code and avoid repeated SQL queries
docs_dict = dict((d.pk, d) for d in docs)
doc_ids = docs_dict.keys()
@ -304,7 +302,9 @@ def retrieve_search_results(form, all_types=False):
# evaluate and fill in attribute results immediately to cut down
# the number of queries
results = list(docs.select_related("states", "ad", "ad__person", "std_level", "intended_std_level", "group", "stream")[:MAX])
docs = docs.select_related("ad", "ad__person", "std_level", "intended_std_level", "group", "stream")
docs = docs.prefetch_related("states__type", "tags")
results = list(docs[:MAX])
fill_in_search_attributes(results)

View file

@ -96,7 +96,9 @@ def file_types_for_drafts():
def all_id2_txt():
# this returns a lot of data so try to be efficient
drafts = Document.objects.filter(type="draft").exclude(name__startswith="rfc").order_by('name').select_related('group', 'group__parent', 'ad', 'ad__email', 'intended_std_level', 'shepherd', 'shepherd__email')
drafts = Document.objects.filter(type="draft").exclude(name__startswith="rfc").order_by('name')
drafts = drafts.select_related('group', 'group__parent', 'ad', 'ad__email', 'intended_std_level', 'shepherd', 'shepherd__email')
drafts = drafts.prefetch_related("states")
rfc_aliases = dict(DocAlias.objects.filter(name__startswith="rfc",
document__states=State.objects.get(type="draft", slug="rfc")).values_list("document_id", "name"))