findClips: select_related('item') / ('item__sort')

Clip.public_id uses self.item.public_id.

Clip.json() uses self.item.sort, so we should select_related on that
rather than the clip's own sort field. (They are identical objects. Is
Clip.sort ever used directly?)

With this change, findClips() issues one query to fetch clips plus one
query per flavour of annotation; before, it issued two extra queries per
clip.
This commit is contained in:
Will Thompson 2016-04-18 15:46:33 +01:00
parent 6dbb7f921a
commit ba00bcbf7b
No known key found for this signature in database
GPG key ID: 3422DC0D7AD482A7

View file

@ -95,6 +95,7 @@ def findClips(request, data):
if 'keys' in data: if 'keys' in data:
qs = order_query(qs, query['sort']) qs = order_query(qs, query['sort'])
qs = qs[query['range'][0]:query['range'][1]] qs = qs[query['range'][0]:query['range'][1]]
qs = qs.select_related('item')
ids = [] ids = []
layers = settings.CONFIG['layers'] layers = settings.CONFIG['layers']
@ -102,7 +103,7 @@ def findClips(request, data):
layer_ids = [k['id'] for k in layers] layer_ids = [k['id'] for k in layers]
keys = filter(lambda k: k not in layer_ids + ['annotations'], data['keys']) keys = filter(lambda k: k not in layer_ids + ['annotations'], data['keys'])
if filter(lambda k: k not in models.Clip.clip_keys, keys): if filter(lambda k: k not in models.Clip.clip_keys, keys):
qs = qs.select_related('sort') qs = qs.select_related('item__sort')
def add(p): def add(p):
ids.append(p.id) ids.append(p.id)