Fixed really slow SQL
This commit is contained in:
parent
8b0ce6ad33
commit
1295144681
|
@ -443,8 +443,12 @@ class ManageAlbumSerializer(
|
||||||
"artist",
|
"artist",
|
||||||
"attributed_to",
|
"attributed_to",
|
||||||
"tags",
|
"tags",
|
||||||
|
"tracks_count",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def get_tracks_count(self, o):
|
||||||
|
return len(o.tracks.all())
|
||||||
|
|
||||||
def get_tags(self, obj):
|
def get_tags(self, obj):
|
||||||
tagged_items = getattr(obj, "_prefetched_tagged_items", [])
|
tagged_items = getattr(obj, "_prefetched_tagged_items", [])
|
||||||
return [ti.tag.name for ti in tagged_items]
|
return [ti.tag.name for ti in tagged_items]
|
||||||
|
|
|
@ -128,7 +128,7 @@ class ManageAlbumViewSet(
|
||||||
music_models.Album.objects.all()
|
music_models.Album.objects.all()
|
||||||
.order_by("-id")
|
.order_by("-id")
|
||||||
.select_related("attributed_to", "artist", "attachment_cover")
|
.select_related("attributed_to", "artist", "attachment_cover")
|
||||||
.with_tracks_count()
|
.prefetch_related("tracks")
|
||||||
)
|
)
|
||||||
serializer_class = serializers.ManageAlbumSerializer
|
serializer_class = serializers.ManageAlbumSerializer
|
||||||
filterset_class = filters.ManageAlbumFilterSet
|
filterset_class = filters.ManageAlbumFilterSet
|
||||||
|
|
|
@ -101,7 +101,7 @@ class ArtistAlbumSerializer(serializers.Serializer):
|
||||||
return o.artist_id
|
return o.artist_id
|
||||||
|
|
||||||
def get_tracks_count(self, o):
|
def get_tracks_count(self, o):
|
||||||
return o._tracks_count
|
return len(o.tracks.all())
|
||||||
|
|
||||||
def get_is_playable(self, obj):
|
def get_is_playable(self, obj):
|
||||||
try:
|
try:
|
||||||
|
@ -210,7 +210,7 @@ class AlbumSerializer(OptionalDescriptionMixin, serializers.Serializer):
|
||||||
return serialize_artist_simple(o.artist)
|
return serialize_artist_simple(o.artist)
|
||||||
|
|
||||||
def get_tracks_count(self, o):
|
def get_tracks_count(self, o):
|
||||||
return getattr(o, "_tracks_count", None)
|
return len(o.tracks.all())
|
||||||
|
|
||||||
def get_is_playable(self, obj):
|
def get_is_playable(self, obj):
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -152,8 +152,10 @@ class ArtistViewSet(
|
||||||
|
|
||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
queryset = super().get_queryset()
|
queryset = super().get_queryset()
|
||||||
albums = models.Album.objects.with_tracks_count().select_related(
|
albums = (
|
||||||
"attachment_cover"
|
models.Album.objects.with_tracks_count()
|
||||||
|
.select_related("attachment_cover")
|
||||||
|
.prefetch_related("tracks")
|
||||||
)
|
)
|
||||||
albums = albums.annotate_playable_by_actor(
|
albums = albums.annotate_playable_by_actor(
|
||||||
utils.get_actor_from_request(self.request)
|
utils.get_actor_from_request(self.request)
|
||||||
|
@ -179,10 +181,10 @@ class AlbumViewSet(
|
||||||
viewsets.ReadOnlyModelViewSet,
|
viewsets.ReadOnlyModelViewSet,
|
||||||
):
|
):
|
||||||
queryset = (
|
queryset = (
|
||||||
models.Album.objects.all()
|
models.Album.objects.all().order_by("-creation_date")
|
||||||
.order_by("-creation_date")
|
# we do a prefetech related on tracks instead of a count because it's more efficient
|
||||||
.with_tracks_count()
|
# db-wise
|
||||||
.prefetch_related("artist", "attributed_to", "attachment_cover")
|
.prefetch_related("artist", "attributed_to", "attachment_cover", "tracks")
|
||||||
)
|
)
|
||||||
serializer_class = serializers.AlbumSerializer
|
serializer_class = serializers.AlbumSerializer
|
||||||
permission_classes = [oauth_permissions.ScopePermission]
|
permission_classes = [oauth_permissions.ScopePermission]
|
||||||
|
@ -759,7 +761,7 @@ class Search(views.APIView):
|
||||||
"album",
|
"album",
|
||||||
queryset=models.Album.objects.select_related(
|
queryset=models.Album.objects.select_related(
|
||||||
"artist", "attachment_cover", "attributed_to"
|
"artist", "attachment_cover", "attributed_to"
|
||||||
),
|
).prefetch_related("tracks"),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
|
@ -373,6 +373,7 @@ def test_manage_nested_artist_serializer(factories, now, to_api_date):
|
||||||
|
|
||||||
def test_manage_album_serializer(factories, now, to_api_date):
|
def test_manage_album_serializer(factories, now, to_api_date):
|
||||||
album = factories["music.Album"](attributed=True, with_cover=True)
|
album = factories["music.Album"](attributed=True, with_cover=True)
|
||||||
|
factories["music.Track"](album=album)
|
||||||
setattr(album, "_tracks_count", 42)
|
setattr(album, "_tracks_count", 42)
|
||||||
expected = {
|
expected = {
|
||||||
"id": album.id,
|
"id": album.id,
|
||||||
|
@ -389,7 +390,7 @@ def test_manage_album_serializer(factories, now, to_api_date):
|
||||||
album.attributed_to
|
album.attributed_to
|
||||||
).data,
|
).data,
|
||||||
"tags": [],
|
"tags": [],
|
||||||
"tracks_count": 42,
|
"tracks_count": 1,
|
||||||
}
|
}
|
||||||
s = serializers.ManageAlbumSerializer(album)
|
s = serializers.ManageAlbumSerializer(album)
|
||||||
|
|
||||||
|
|
|
@ -276,7 +276,7 @@
|
||||||
</router-link>
|
</router-link>
|
||||||
</td>
|
</td>
|
||||||
<td>
|
<td>
|
||||||
{{ object.tracks.length }}
|
{{ object.tracks_count }}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
|
|
Loading…
Reference in New Issue