database - Django Query extremely slow -
i got problem django application. queries on model scope extremly slow , after debugging still got no clue problem is.
when query db scope = scope.objects.get(pk='esoterik i')
in django takes 5 10 seconds. database has less 10 entries , index on primary key. way slow. when executing equivalent query on db select * scope title='esoterik i';
ok, takes 50ms.
same problem happens if query set of results scope_list = scope.objects.filter(members=some_user)
, doing e.g. print(scope_list) or iterating on elements of list. query takes few ms print or iterating of elements takes again 5 10 seconds set has 2 entries.
database backend postgresql. problem occurs same on local development server , apache.
here code of model:
class scope(models.model): title = models.charfield(primary_key=true, max_length=30) ## semester scope linked assoc_semester = models.foreignkey(semester, null=true) ## grade of scope. can null if scope not class assoc_grade = models.foreignkey(grade, null=true) ## timetable of scope. can null if scope not direct associated class assoc_timetable = models.foreignkey(timetable, null=true) ## associated subject of scope assoc_subject = models.foreignkey(subject) ## calendar of scope assoc_calendar = models.foreignkey(calendar) ## usergroup of scope assoc_usergroup = models.foreignkey(group) members = models.manytomanyfield(user) unread_count = none
update here output of python profiler. seems query.py getting called 1.6 million times little much.
you should try , first isolate problem. run manage.py shell , run following:
scope = scope.objects.get(pk='esoterik i') print scope
now django queries not executed until have to. say, if you're experiencing slowness after first line, problem somewhere in creation of query suggest problems object manager. next step try , execute raw sql through django, , make sure problem manager , not bug in django in general.
if you're experiencing slowness second line, problem eitherwith actual execution of query, or display\printing of data. can force-execute query without printing (check documentation) find out 1 is.
that's far understand think best way solve break process down different parts , finding out part 1 causing slowness
Comments
Post a Comment