如何使用Django调试工具栏debugsqlsh打印sql执行的总时间

2024-04-19 02:03:57 发布

您现在位置:Python中文网/ 问答频道 /正文

我从django工具栏找到了debugsqlshell,您可以使用它来运行它管理.py作为标准shell的替代,我想测试嵌套对象上查询的性能。在

我在每个查询上都可以看到certian输出,但是如何获得所有查询所用时间的总和呢?在

下面两种方法,我想比较一下。在

def stands(self):
    stands = []
    for stand in Stand.objects.all():
        if stand.room.floor.building.fair.pk == self.pk:
            stands.append(stand)

    return stands

第二次使用prefetch_related

^{pr2}$

多亏了joel goldstick的提议,cProfile我设法得到了一些确切的数字:

站起来():

11 685 649 function calls (10 532 897 primitive calls) in 9.861 seconds

stands3()使用prefetch_related

1 855 483 function calls (1 839 617 primitive calls) in 1.295 seconds

但是仍然从debugshell获得这些数字是很酷的,所以如果你知道怎么做就不用费心回答了。在


Tags: djangoinselffunction数字工具栏secondsrelated