Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在Django shell会话期间获取SQL查询计数_Python_Sql_Django_Shell - Fatal编程技术网

Python 在Django shell会话期间获取SQL查询计数

Python 在Django shell会话期间获取SQL查询计数,python,sql,django,shell,Python,Sql,Django,Shell,有没有办法打印Django ORM在Django shell会话期间执行的原始SQL查询的数量 Django调试工具栏已经提供了此类信息(例如,5.83MS中的5个查询),但如何从shell中获取这些信息并不明显。您可以使用连接。查询: >>> from django.conf import settings >>> settings.DEBUG = True >>> from django.db import connection >

有没有办法打印Django ORM在Django shell会话期间执行的原始SQL查询的数量


Django调试工具栏已经提供了此类信息(例如,5.83MS中的5个查询),但如何从shell中获取这些信息并不明显。

您可以使用
连接。查询

>>> from django.conf import settings
>>> settings.DEBUG = True
>>> from django.db import connection
>>> Model.objects.count()
>>> # python 3 uses print()
>>> print(len(connection.queries))
1

这是对公认答案的一点改进。在某些应用程序中创建一个名为extra_imports.py的python文件(例如
some_app

额外_.py

from django.conf import settings
settings.DEBUG = True
from django.db import connection, reset_queries


def num_queries(reset=True):
    print(len(connection.queries))
    if reset:
        reset_queries()
现在,如果您正在使用django_扩展中的shell_plus(顺便说一句,如果您不使用它,请检查它),请在settings.py中添加以下行

SHELL_PLUS_PRE_IMPORTS = [('some_app.extra_imports', '*')]
如果您使用的是django shell,请在shell内部运行此命令

exec(open('some_app/extra_imports.py').read()) # python3
execfile('some_app/extra_imports.py').read()) # pyhton2
现在,


如果您有数据库路由和多个连接,那么计算数据库命中率会有点困难,因为
connection.querys
据我所知,只考虑默认连接

要包括所有连接,请执行以下操作:

from django.db import connections,connection,reset_queries
from django.conf import settings
settings.DEBUG = True
...
def query_count_all()->int:
    query_total = 0
    for c in connections.all():
        query_total += len(c.queries)
    return query_total
或者更简洁地说:

def query_count_all()->int:
   return sum(len(c.queries) for c in connections.all())

reset_查询()
已经处理了多个连接

不幸的是,Django 1.5似乎不起作用,查询计数保持在0。使用Django 1.11测试此方法,它工作得非常好!我的一个go-to。这也很有用:
Django.db.reset\u查询
重置该计数。注意,这将在9000处停止计数。请参阅
BaseDatabaseWrapper.querys\u limit
即使在Django 1.11中,这也不适用于我。我必须使用Tim Richardson的答案来获得与实际查询的联系。
def query_count_all()->int:
   return sum(len(c.queries) for c in connections.all())