记录使用DEBUG设置为False的Django SQL查询 [英] Logging Django SQL queries with DEBUG set to False

查看:172
本文介绍了记录使用DEBUG设置为False的Django SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有可能通过查看 connection.queries django-debug-toolbar 也有助于开发许多。

I know that it is possible to get all the SQL queries that were run for the current request/response when DEBUG is on by looking at connection.queries. The django-debug-toolbar also helps a lot on development.

问题是我的生产服务器处于高负载状态,我想记录正在为每个视图执行的查询,以便我可以优化首先创建更多查询的页面。

The problem is that my production server is under high load and I would like to log the queries that are being executed for each view so I can optimize the pages that are creating more queries first.

可以不修改我的数据库驱动程序吗?

Is it possible to do that without modifying my database driver?

推荐答案

Django 1.3,我在 django / db / backends / __ init __。py 中看到类 BaseDatabaseWrapper 具有一个属性 use_debug_cursor 。该类是表示默认数据库连接的 django.db.connection 对象的包装器( docs )。似乎将此属性设置为true将导致Django使用 CursorDebugWrapper django / db / backends / util.py ,它记录它执行的所有查询,而不是 CursorWrapper ,而不是。

In Django 1.3, I see class BaseDatabaseWrapper in django/db/backends/__init__.py has an attribute use_debug_cursor. This class is the wrapper for the django.db.connection object that represents the default database connection (docs). Seems like setting this attribute to true would cause Django to use a CursorDebugWrapper from django/db/backends/util.py, which logs all queries it executes, instead of a CursorWrapper, which does not.

use_debug_cursor 不存在于Django 1.2中,这是我现在在这台机器上安装的,所以我现在无法测试。如果我有几分钟的时间,我会尝试得到一个虚拟1.3项目,开始测试这个...我可能会在我的头上!

use_debug_cursor is not present in Django 1.2, which is what I have installed on this machine at the moment, so I can't test it right now. If I have a few minutes, I'll try to get a dummy 1.3 project started to test this out...I may well be in over my head here!

这篇关于记录使用DEBUG设置为False的Django SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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