Optimized list viewsets for annotations and large amounts of data #9054
Unanswered
sebastien-grdn
asked this question in
Ideas & Suggestions
Replies: 2 comments
-
as every projects need is different, hence DRF let us to override its built in methods as needed. I think your approach is OK. |
Beta Was this translation helpful? Give feedback.
0 replies
-
I think you can also speed up by using Im considering doing something similar where it only annotates counts when ordering is specified in query string but I know I will get exception bc it wont be possible to put it in
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello everyone,
I'd like to have feedback from the community about a performance issue (and a workaround I came up with) I've had in a list endpoint with a lot of annotations (too much for the data to be denormalized inside the model), done inside the
get_queryset
method, and that are used in adjango-filter
filterset, and of course in the serializer. I also have a lot of instances to be filtered. That led to minute long requests.So my first question is: was I doing it wrong? I'm using
GenericViewSet
,ListModelMixin
and the non-model fields used inside the serializer all come from annotations, they're not loaded during the serialization.I ended up using a new strategy, overriding the
get_queryset
andlist
methods (fromGenericViewSet
andListModelMixin
).I'm posting the code lower but tl;dr the idea was to:
.values_list()
, only retrieve ids from the databaseWith this method I managed to speed up the request about 20x.
Here is the piece of code, as an example implementation of this strategy. Any feedback or questions welcome. Thanks a lot!
Beta Was this translation helpful? Give feedback.
All reactions