Django: Optimization 1
Django: Optimization 1
Official Django logo
Last week August 03–07, 2020 had the task of optimization of backend.
TLDR; In optimization of Django backend, try to number of queries as well as reduce file write operation.
When profiling, using django-silk, found APIs where each had like 1000 queries per call. These APIs had a typical pattern in the code. Like:
Pattern found in code
This logic fires 2 queries. One to check for whether an object exists with the provided condition and if so, for processing of queryset. The overall result expected from such function is some processed data or None.
Calling such functions 1000 times would result in at least 2000 queries assuming that in all case queryset exists.
Now a possible solution is try/except handling, as per below code shot.
Will reduce the number of queries
Fires only one query, irrespectively.
When the functions are converted into the above pattern and deployed, found, even though queries have reduced, APIs are started to give frequent timeouts.
Cause found to be massive logging file write operations on exception. Just commented that part.
Will avoid file operations
This achieved fast API with fewer queries with no timeout errors.