#request_finished but not loading...

4 messages · Page 1 of 1 (latest)

clear nest
#

I am totally stumped. I have two models (Company and Customer). I am trying to get a list of all companies that have served this particular customer, but for legacy DB reasons, I can't use foreign keys and am instead relying on a bit of a funky materialized view setup. But for reasons that are absolutely baffling to me, DRF is taking forever to manage the request when there are a large number of customers (User CPU time 145203.809 msec) even though the SQL query is very fast... (329.21 ms).

Here's the code. Any idea why DRF would be choking?

class CompanyListView(generics.ListAPIView):
    def get_queryset(self):
        queryset = Company.objects.all()
        if 'customer' in self.request.query_params:
            customer = self.request.query_params["customer"]
            companies = MaterializedViewLookupTable.objects.filter(customer=customer).values_list(
                "company_id", flat=True
            )
            queryset = queryset.filter(
                company_id__in=[*companies],
            )
        return queryset

    lookup_url_kwarg = "company_id"
    serializer_class = CompanySeralizer
    filterset_class = CompanyFilter
    pagination_class = StandardResultsSetPagination
    permission_classes = [permissions.IsAuthenticated]
junior stump
#

Have you tried using a profiler?

Also ```python
queryset.filter(
company_id__in=[*companies],
)

May not be what you want. This is evaluating `companies` into an actual list. If you use: `company_id__in=companies` django will cause it to be a nested SQL query probably improving the performance.
clear nest
#

OMG... it's possible that the list was the root cause. i just tried it without it and the performance drop went away

#

Wow. @junior stump THANK YOU! This was driving me bananas for about 24 hours and you fixed it in like 15 seconds. ❤️