Closed rishireward closed 6 months ago
@yatishTrootech please coordinate with @suyeshkandpal28 @VipinRewardz @thickkoezz on what fields they would be needing on the UI on the listing page.
PostViewSet (api/posts)
Previous
Now
New 778 ms
Old 2.83 seconds
OLD 2.36 seconds
New 745ms
cc: @suryaiiit @rishireward
there's pagination, but that's also not going to help, as we're returning too many related fields in that response
For one single object retrieval, we're hitting 15 different queries, that's quite a lot. Out of these 15, there's one query which takes around 5.09 seconds to complete, so for 20 different feeds on single page, it takes around 250 queries
IMO, we must create one light serializer for the list view, as I'm pretty sure we're not showing all the fields in the list view. We return only those fields that we show in the UI while listing. Return more fields to the detail page only if needed.
Try to use, select_related and prefetch_related for the related fields. Also, I noticed that in serializer, we're hitting queries like this Documents.objects.filter(post=post_id), instead, we should use the related query
Queries running for one object when sent to serializer
The idea here is to create a separate serializer for list view and detail view; check with the mobile team what fields they're showing in the list page, and we will send only those fields. We will also use select_related and prefetch_related for fetching related models. Also, ensure to avoid queries like
Documents.objects.filter(post=post_id)
in the serializer's get_field method, instead, retrieve using a relation that is prefetched to avoid an N + 1 query. Please create a fresh serializer for this; do not inherit it from previous serializers.