Optimize some management endpoints via the querysets#1600
Conversation
…and exports Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
…or catalog index Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
…sets Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
| "queries": { | ||
| "index": 3, | ||
| "list": 9, | ||
| "detail": 9, |
There was a problem hiding this comment.
this mapper defines the max query counts per api endpoint action during a test.
Very simple test at the moment. They could be expanded by creating 10000 Options, Attributes, etc dynamically during test as well..
| 'editors' | ||
| ) | ||
|
|
||
| def get_queryset(self): |
There was a problem hiding this comment.
nearly forgot the Tasks and Views ;)
jochenklar
left a comment
There was a problem hiding this comment.
I am a bit puzzled, why I cannot follow part of your optimizations, maybe we should talks next week.
| queryset = Attribute.objects.all().order_by('path') | ||
| if self.action in ['index']: | ||
| return queryset | ||
| elif self.action in ('nested', 'export', 'detail_export'): |
There was a problem hiding this comment.
I don't think this is needed since we use the mptt tree here.
There was a problem hiding this comment.
For the nested view, editors causes one query per Attribute to Site and this does not work with prefetch_related due to how the MPTT-Tree works. However we could just remove editors from the nested serializer.
| elif self.action in ('nested', 'export', 'detail_export'): | ||
| return queryset.select_related('parent') | ||
| else: | ||
| return queryset.annotate( |
There was a problem hiding this comment.
This is actually not needed since the list serializer does not use the prefetched fields and for one object there is no gain. But it was like this before, so i would keep it. Just remove the parent.
| } | ||
|
|
||
|
|
||
| def get_params(action): |
There was a problem hiding this comment.
I think the dict above could be in a shape that we dont need this function.
There was a problem hiding this comment.
Should be named test_api_performance.py or test_max_num_queries.py. This is very interesting, I was not aware of django_assert_max_num_queries.
There was a problem hiding this comment.
I think this should be distributed to the different django apps, which would reduce the complexity a bit.
| 'catalog_sections__section__section_pages__page__page_questions__question__attribute', | ||
| 'catalog_sections__section__section_pages__page__page_questions__question__conditions', | ||
| 'catalog_sections__section__section_pages__page__page_questions__question__optionsets', | ||
| 'catalog_sections__section__section_pages__page__page_questions__question__default_option', |
There was a problem hiding this comment.
I am not convinced prefetching the default_option is needed, since we only use its id in the serializer and do not resolve the instance.
There was a problem hiding this comment.
And what about the QuestionExportSerializer? It uses default_option = serializers.CharField(source='default_option.uri', default=None, read_only=True) so default_option.uri needs to be resolved for exports right?! It would be used by all Export serializers up to the CatalogExport. Would it be worth to add the prefetch field only for export or apply it only for the export actions? Otherwise we can leave it out like you said.
rdmo/rdmo/questions/serializers/export.py
Line 26 in e77e2ca
There was a problem hiding this comment.
So actually, the export of a Catalog via the management interface can make almost 3000(!) queries and debug toolbar crashed my machine a bit.
Is it because of a missing prefetch_elements call in the detail_export?
rdmo/rdmo/questions/viewsets.py
Line 97 in e77e2ca
| 'questionsets', | ||
| 'editors' | ||
| ).select_related('attribute') | ||
| ).select_related('attribute', 'default_option') |
There was a problem hiding this comment.
Same as above, I don't think we use this.
…elated Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
…unctinos and select_related calls Signed-off-by: David Wallace <david.wallace@tu-darmstadt.de>
|
so, these were the query counts from before the optimizations of the For a catalog 😅 : |
Description
Related issue: "I think we had an issue from even before the migration to the React interface"
Some managment pages were still loading slowly, one prefetch field
default_optionfor the questions.Models was missing. This may improve some queries.