From d7f087125ed23dc157e8402a7032095fddebf3db Mon Sep 17 00:00:00 2001 From: j Date: Fri, 18 Aug 2023 14:59:45 +0200 Subject: [PATCH] speed up stream lookup --- ...007_stream_archive_str_file_id_69a542_idx.py | 17 +++++++++++++++++ pandora/archive/models.py | 3 +++ pandora/item/models.py | 9 ++++----- 3 files changed, 24 insertions(+), 5 deletions(-) create mode 100644 pandora/archive/migrations/0007_stream_archive_str_file_id_69a542_idx.py diff --git a/pandora/archive/migrations/0007_stream_archive_str_file_id_69a542_idx.py b/pandora/archive/migrations/0007_stream_archive_str_file_id_69a542_idx.py new file mode 100644 index 000000000..4a8bb7e2f --- /dev/null +++ b/pandora/archive/migrations/0007_stream_archive_str_file_id_69a542_idx.py @@ -0,0 +1,17 @@ +# Generated by Django 4.2.3 on 2023-08-18 12:54 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('archive', '0006_alter_file_extension_alter_file_id_alter_file_info_and_more'), + ] + + operations = [ + migrations.AddIndex( + model_name='stream', + index=models.Index(fields=['file', 'source', 'available'], name='archive_str_file_id_69a542_idx'), + ), + ] diff --git a/pandora/archive/models.py b/pandora/archive/models.py index 17c9d67a6..affdd45ac 100644 --- a/pandora/archive/models.py +++ b/pandora/archive/models.py @@ -734,6 +734,9 @@ class Stream(models.Model): class Meta: unique_together = ("file", "resolution", "format") + indexes = [ + models.Index(fields=['file', 'source', 'available']) + ] file = models.ForeignKey(File, related_name='streams', on_delete=models.CASCADE) resolution = models.IntegerField(default=96) diff --git a/pandora/item/models.py b/pandora/item/models.py index 5c66e860f..7b1939a41 100644 --- a/pandora/item/models.py +++ b/pandora/item/models.py @@ -644,7 +644,7 @@ class Item(models.Model): i['hasSource'] = self.streams().exclude(file__data='').exists() streams = self.streams() - i['durations'] = [s.duration for s in streams] + i['durations'] = [s[0] for s in streams.values_list('duration')] i['duration'] = sum(i['durations']) i['audioTracks'] = self.audio_tracks() if not i['audioTracks']: @@ -1309,10 +1309,9 @@ class Item(models.Model): return sorted(set(tracks)) def streams(self, track=None): + files = self.files.filter(selected=True).filter(Q(is_audio=True) | Q(is_video=True)), qs = archive.models.Stream.objects.filter( - source=None, available=True, file__item=self, file__selected=True - ).filter( - Q(file__is_audio=True) | Q(file__is_video=True) + file__in=files, source=None, available=True ).select_related() if not track: tracks = self.audio_tracks() @@ -1561,7 +1560,7 @@ class Item(models.Model): if not subtitles: return # otherwise add empty 5 seconds annotation every minute - duration = sum([s.duration for s in self.streams()]) + duration = sum([s[0] for s in self.streams().values_list('duration')]) layer = subtitles['id'] # FIXME: allow annotations from no user instead? user = User.objects.all().order_by('id')[0]