Django查看响应时间问题 [英] Django view response time issues

查看:586
本文介绍了Django查看响应时间问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多从查询中得到的对象,查询器还没有被评估,当我将objectlist传递给paginator对象时,需要14秒才能返回paginator对象,这是因为它正在评估list中的所有对象花费时间(击中db可能是)。



我强力评估了查询集,然后将其发送到分页对象:

  ds_objects = list(ds_objects)

猜猜现在分页返回对象为0.0秒,但由ds_objects = list(ds_objects)采取的所有时间仍然存在问题,我如何有效地执行此操作,以使我的视图响应时间缩短到2或3秒钟?我没有找到任何最佳解决方案:s

解决方案

我的猜测是JOIN(所以 select_related )对查询速度不重要。无论如何,当您执行 values()查询时,您不需要真正需要 select_related ,因为您将只有唯一的字段get被指定为 values()参数,并且它们都被JOINs检索。



看看为什么需要很长时间,您可以执行以下操作:



使用

打印出raw sql

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

修改它(实际上不是SQL,但应该足够近)要在SQL客户端中运行,如django自己的 manage.py dbshel​​l 。 / p>

执行它,记下时间。执行然后 EXPLAIN ... (将查询放在点上),您可能会看到一些全表扫描消息



在数据库表中,在受影响的列上添加适当的索引,然后再次执行该查询。索引可能是这样的:

  CREATE INDEX idx_action_log_on_tkid_logid ON tbl_action_log(TicketID,ActionLogID); 

当您对索引感到满意时,将其创建的逗号存储在
文件 app / sql / modelname.sql ,这将在任何新的部署中创建索引。


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

解决方案

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.

这篇关于Django查看响应时间问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆