Hello,

We have a lot of these type of thing throughout our code:

class PropertyViewSet(V2Mixin, V2BulkUpdateViewSet):
    queryset = (
        Property.objects.all()
        .prefetch_related(
            'servicecontract_set',
            'servicecontract_set__service',
            'client__primary_contact',
            'client__tags',
            'tags',
        )
        .annotate(inspectionitem_count=Count('inspectionitem'))
    )

The .annotate was fine when our dataset was small, but now it's become a 
real performance issue. It adds about 500ms to the query time and 
surprisingly 4seconds to the page load time, I suspect due to the ORM doing 
so much busywork on the data that comes via the join.

Does anyone have an idea for doing fast annotations of only the current 
page of data? e.g. it's trivial to issue a separate (and fast) query 
against the InspectionItem table grouping by the Property to give us the 
counts for e.g. the 100 results to be displayed on the current page. I was 
thinking some sort of SerializerMethodField which primed a cache on first 
hit.

Has anyone done anything like this?

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to