I am having a problem with a Django application. Scope model requests are extremely slow, and after some debugging, I still haven't figured out where the problem is.
When I request db as scope = Scope.objects.get(pk='Esoterik I') in django, it takes 5-10 seconds. The database contains less than 10 records and a primary key index. therefore it is too slow. When executing an equivalent query on db, as SELECT * FROM scope WHERE title='Esoterik I'; everything is in order, it takes only about 50 ms.
The same problem occurs if I execute a query with a set of results, such as scope_list = Scope.objects.filter(members=some_user) , and then execute, for example. print (scope_list) or iterate over list items. The request itself takes only a few ms, but the fingerprint or iteration of the elements is repeated in 5-10 seconds, but the set has only two entries.
Backend Database - Postgresql. The problem occurs the same on the local development server and apache.
here is the model code:
class Scope(models.Model): title = models.CharField(primary_key=True, max_length=30)
The update here is the output of the python profiler. query.py seems to be called 1.6 million times, that's too much. 
source share