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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19556139/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 14:02:34  来源:igfitidea点击:

Get SQL query count during a Django shell session

pythonsqldjangoshell

提问by Jian

Is there a way to print the number of raw SQL queries performed by the Django ORM during a Django shell session?

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

This sort of information is already provided by the Django debug toolbar (e.g, 5 QUERIES in 5.83MSbut it's not obvious how to get it from the shell.

这种信息已经由 Django 调试工具栏提供(例如,5 QUERIES in 5.83MS但如何从 shell 获取它并不明显。

采纳答案by Jian

You can use connection.queries:

您可以使用connection.queries

>>> 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

回答by Nithin

This is a slight improvement on the accepted answer. create a python file named extra_imports.py in some app (Eg some_app)

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

extra_imports.py

extra_imports.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()

Now, If you are using shell_plus from django_extension (btw check it out if you are not using it), add following line to settings.py

现在,如果您正在使用 django_extension 中的 shell_plus(顺便说一句,如果您不使用它,请检查它),将以下行添加到 settings.py

SHELL_PLUS_PRE_IMPORTS = [('some_app.extra_imports', '*')]

If you are using django shell, run this inside shell

如果您使用的是 django shell,请在 shell 中运行它

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

Now,

现在,

In [1]: User.objects.all()
In [2]: num_queries()
1

In [3]: User.objects.filter(company=Company.objects.first()).all()
In [4]: num_queries()
2

回答by Tim Richardson

If you have database routing and multiple connections, it's a bit trickier to count your database hits because connection.queriesconsiders only the default connection, as far as I can tell.

如果你有数据库路由和多个连接,计算你的数据库命中有点棘手connection.queries,因为据我所知,只考虑默认连接。

To include all connections:

要包括所有连接:

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

or more concisely:

或更简洁地说:

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

reset_queries()already handles multiple connections

reset_queries()已经处理多个连接