Skip to content

Commit defb116

Browse files
committed
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
1 parent a656cf8 commit defb116

3 files changed

Lines changed: 9 additions & 7 deletions

File tree

ietf/doc/models.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def get_state(self, state_type=None):
122122

123123
if not hasattr(self, "state_cache") or self.state_cache == None:
124124
self.state_cache = {}
125-
for s in self.states.all().select_related("type"):
125+
for s in self.states.all():
126126
self.state_cache[s.type_id] = s
127127

128128
return self.state_cache.get(state_type, None)
@@ -357,7 +357,7 @@ def friendly_state(self):
357357
iesg_state = self.get_state("draft-iesg")
358358
iesg_state_summary = None
359359
if iesg_state:
360-
iesg_substate = self.tags.filter(slug__in=IESG_SUBSTATE_TAGS)
360+
iesg_substate = [t for t in self.tags.all() if t.slug in IESG_SUBSTATE_TAGS]
361361
# There really shouldn't be more than one tag in iesg_substate, but this will do something sort-of-sensible if there is
362362
iesg_state_summary = iesg_state.name
363363
if iesg_substate:

ietf/doc/views_search.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ def wrap_value(v):
112112

113113
def fill_in_search_attributes(docs):
114114
# fill in some attributes for the search results to save some
115-
# hairy template code and avoid repeated SQL queries - remaining
116-
# queries we don't handle here are mostly implicit many-to-many
117-
# relations for which there is poor support in Django 1.2
115+
# hairy template code and avoid repeated SQL queries
118116

119117
docs_dict = dict((d.pk, d) for d in docs)
120118
doc_ids = docs_dict.keys()
@@ -304,7 +302,9 @@ def retrieve_search_results(form, all_types=False):
304302

305303
# evaluate and fill in attribute results immediately to cut down
306304
# the number of queries
307-
results = list(docs.select_related("states", "ad", "ad__person", "std_level", "intended_std_level", "group", "stream")[:MAX])
305+
docs = docs.select_related("ad", "ad__person", "std_level", "intended_std_level", "group", "stream")
306+
docs = docs.prefetch_related("states__type", "tags")
307+
results = list(docs[:MAX])
308308

309309
fill_in_search_attributes(results)
310310

ietf/idindex/index.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ def file_types_for_drafts():
9696
def all_id2_txt():
9797
# this returns a lot of data so try to be efficient
9898

99-
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')
99+
drafts = Document.objects.filter(type="draft").exclude(name__startswith="rfc").order_by('name')
100+
drafts = drafts.select_related('group', 'group__parent', 'ad', 'ad__email', 'intended_std_level', 'shepherd', 'shepherd__email')
101+
drafts = drafts.prefetch_related("states")
100102

101103
rfc_aliases = dict(DocAlias.objects.filter(name__startswith="rfc",
102104
document__states=State.objects.get(type="draft", slug="rfc")).values_list("document_id", "name"))

0 commit comments

Comments
 (0)