From aed064d7f297a216c7b4bbbda1e1e6948d8ee808 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Fri, 14 Oct 2011 00:59:57 +0200 Subject: [PATCH] distinct makes clip queries slow --- pandora/clip/managers.py | 1 - pandora/clip/models.py | 9 +++++++++ pandora/clip/views.py | 9 +++++---- pandora/item/managers.py | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/pandora/clip/managers.py b/pandora/clip/managers.py index 42e553a67..e06bb4f68 100644 --- a/pandora/clip/managers.py +++ b/pandora/clip/managers.py @@ -171,5 +171,4 @@ class ClipManager(Manager): filter_layers = filter(lambda k: k in public_layers, data['keys']) if filter_layers: qs = qs.filter(annotations__layer__name__in=filter_layers) - qs = qs.distinct() return qs diff --git a/pandora/clip/models.py b/pandora/clip/models.py index 756090ac4..5f5bf85ff 100644 --- a/pandora/clip/models.py +++ b/pandora/clip/models.py @@ -9,6 +9,10 @@ from archive import extract import managers class Clip(models.Model): + ''' + CREATE INDEX clip_clip_title_idx ON clip_clip (title ASC NULLS LAST); + CREATE INDEX clip_clip_director_idx ON clip_clip (director ASC NULLS LAST); + ''' class Meta: unique_together = ("item", "start", "end") @@ -32,6 +36,9 @@ class Clip(models.Model): lightness = models.FloatField(default=0, db_index=True) volume = models.FloatField(default=0, null=True, db_index=True) + director = models.CharField(max_length=1000) + title = models.CharField(max_length=1000) + def update_calculated_values(self): self.duration = self.end - self.start if self.duration > 0: @@ -41,6 +48,8 @@ class Clip(models.Model): else: self.hue = self.saturation = self.lightness = 0 self.volume = 0 + self.director = self.item.sort.director + self.title = self.item.sort.title def save(self, *args, **kwargs): self.public_id = u"%s/%s-%s" %(self.item.itemId, self.start, self.end) diff --git a/pandora/clip/views.py b/pandora/clip/views.py index 889efc8d9..1484d54e9 100644 --- a/pandora/clip/views.py +++ b/pandora/clip/views.py @@ -20,7 +20,7 @@ def parse_query(data, user): if key in data: query[key] = data[key] query['qs'] = models.Clip.objects.find(query, user) - if 'itemQuery' in data: + if 'itemQuery' in data and data['itemQuery'].get('conditions'): item_query = Item.objects.find({'query': data['itemQuery']}, user) query['qs'] = query['qs'].filter(item__in=item_query) return query @@ -32,7 +32,8 @@ def order_query(qs, sort): if operator != '-': operator = '' clip_keys = ('public_id', 'start', 'end', 'hue', 'saturation', 'lightness', 'volume', - 'annotations__value') + 'annotations__value', + 'director', 'title') key = { 'id': 'public_id', 'in': 'start', @@ -53,7 +54,6 @@ def order_query(qs, sort): order_by.append(order) if order_by: qs = qs.order_by(*order_by, nulls_last=True) - qs = qs.distinct() return qs def findClips(request): @@ -76,7 +76,8 @@ def findClips(request): query = parse_query(data, request.user) qs = order_query(query['qs'], query['sort']) if 'keys' in data: - qs = qs.select_related()[query['range'][0]:query['range'][1]] + qs = qs.select_related() + qs = qs[query['range'][0]:query['range'][1]] response['data']['items'] = [p.json(keys=data['keys']) for p in qs] elif 'position' in query: ids = [i.public_id for i in qs] diff --git a/pandora/item/managers.py b/pandora/item/managers.py index 12714cea0..50e686ce2 100644 --- a/pandora/item/managers.py +++ b/pandora/item/managers.py @@ -242,7 +242,7 @@ class ItemManager(Manager): #only include items that have hard metadata conditions = parseConditions(data.get('query', {}).get('conditions', []), data.get('query', {}).get('operator', '&')) - qs = qs.filter(conditions).distinct() + qs = qs.filter(conditions) #anonymous can only see public items if user.is_anonymous():