Django view response time issues

谁说胖子不能爱 提交于 2019-12-10 15:53:11

问题


Hi i have lots of objects which i get from query, the queryset is not yet evaluated, when i pass objectlist to paginator object it took 14 seconds to return paginator object, it is because it is evaluating the all objects in list which took time (hitting db may be).

I forcefully evaluated queryset before sending it to paginator object as:

ds_objects = list(ds_objects)

Guess what now pagination took 0.0 seconds in returning object, but the problem still remain now all time is taken by ds_objects = list(ds_objects), how can i do it efficiently so that my view response time reduced to just 2 or 3 seconds? i have not found any best solution :s


回答1:


My guess is that the JOINs (so the select_related) are not important for the query speed. Anyway when you do a values() query you don't really need select_related because the only fields you'll ever get are those specified as values() arguments, and they are retrieved by JOINs anyway.

To see why it takes so long, you can do the following:

Print out the "raw sql" with

print tbl_action_log.objects.order_by('-TicketID', '-ActionLogID').query

Modify it (it's not actually SQL, but it should be near enough) to be run in a SQL client, like django's own manage.py dbshell.

Execute it, take note of the time. Execute then EXPLAIN ... (put your query in the dots), you will probably see some "full table scan" message

Add a proper index to the database table, over the affected columns, and execute the query again. The index will probably be this:

CREATE INDEX idx_action_log_on_tkid_logid ON tbl_action_log (TicketID, ActionLogID);

When you are satisfied with the index, store its creation commmand in the file app/sql/modelname.sql, this will create the index in any new deployment.



来源:https://stackoverflow.com/questions/7008249/django-view-response-time-issues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!