Commit 12951446 authored by Agate's avatar Agate 💬

Fixed really slow SQL

parent 8b0ce6ad
......@@ -443,8 +443,12 @@ class ManageAlbumSerializer(
"artist",
"attributed_to",
"tags",
"tracks_count",
]
def get_tracks_count(self, o):
return len(o.tracks.all())
def get_tags(self, obj):
tagged_items = getattr(obj, "_prefetched_tagged_items", [])
return [ti.tag.name for ti in tagged_items]
......
......@@ -128,7 +128,7 @@ class ManageAlbumViewSet(
music_models.Album.objects.all()
.order_by("-id")
.select_related("attributed_to", "artist", "attachment_cover")
.with_tracks_count()
.prefetch_related("tracks")
)
serializer_class = serializers.ManageAlbumSerializer
filterset_class = filters.ManageAlbumFilterSet
......
......@@ -101,7 +101,7 @@ class ArtistAlbumSerializer(serializers.Serializer):
return o.artist_id
def get_tracks_count(self, o):
return o._tracks_count
return len(o.tracks.all())
def get_is_playable(self, obj):
try:
......@@ -210,7 +210,7 @@ class AlbumSerializer(OptionalDescriptionMixin, serializers.Serializer):
return serialize_artist_simple(o.artist)
def get_tracks_count(self, o):
return getattr(o, "_tracks_count", None)
return len(o.tracks.all())
def get_is_playable(self, obj):
try:
......
......@@ -152,8 +152,10 @@ class ArtistViewSet(
def get_queryset(self):
queryset = super().get_queryset()
albums = models.Album.objects.with_tracks_count().select_related(
"attachment_cover"
albums = (
models.Album.objects.with_tracks_count()
.select_related("attachment_cover")
.prefetch_related("tracks")
)
albums = albums.annotate_playable_by_actor(
utils.get_actor_from_request(self.request)
......@@ -179,10 +181,10 @@ class AlbumViewSet(
viewsets.ReadOnlyModelViewSet,
):
queryset = (
models.Album.objects.all()
.order_by("-creation_date")
.with_tracks_count()
.prefetch_related("artist", "attributed_to", "attachment_cover")
models.Album.objects.all().order_by("-creation_date")
# we do a prefetech related on tracks instead of a count because it's more efficient
# db-wise
.prefetch_related("artist", "attributed_to", "attachment_cover", "tracks")
)
serializer_class = serializers.AlbumSerializer
permission_classes = [oauth_permissions.ScopePermission]
......@@ -759,7 +761,7 @@ class Search(views.APIView):
"album",
queryset=models.Album.objects.select_related(
"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):
album = factories["music.Album"](attributed=True, with_cover=True)
factories["music.Track"](album=album)
setattr(album, "_tracks_count", 42)
expected = {
"id": album.id,
......@@ -389,7 +390,7 @@ def test_manage_album_serializer(factories, now, to_api_date):
album.attributed_to
).data,
"tags": [],
"tracks_count": 42,
"tracks_count": 1,
}
s = serializers.ManageAlbumSerializer(album)
......
......@@ -276,7 +276,7 @@
</router-link>
</td>
<td>
{{ object.tracks.length }}
{{ object.tracks_count }}
</td>
</tr>
</tbody>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment