dabadaba
dabadaba

Reputation: 9542

How to exhaustively see queries Django is running?

I am optimizing a big part of my code so I am trying to reduce the database hits to the minimum and try to grab all necessary data in big but few queries and cache it.

However I have not found an effective way of seeing whether Django is actually running a query against the database or whether it is returning cached or prefetched data.

Ideally, I want to debug the relevant parts of my code line by line and each raw SQL query should be displayed as they are executed. Essentially some kind of listener that logs every query that is run as soon as it is run.

This way to check running queries suggested by the docs is not good enough, as it only displays queries formed in the same file where you access connection.queries, apparently.

Accessing QuerySet.query is no good either, as it requires a, well, QuerySet object. There are other scenarios where queries are run against the database but the return type is not a QuerySet.

I am not sure the Django Debug Toolbar would work either, because I don't want my behavior wrapped in a view, I want to execute an independent script and thoroughly debug it.

Upvotes: 2

Views: 1010

Answers (2)

Luan Fonseca
Luan Fonseca

Reputation: 1517

Yes, You should use the django-debug-toolbar for that. It will display each query made on some view, if that code you want is like, inside some Model or other python file and sometime is called for some view. Even if it is as a signal on model, for instance. It will be caught by the debug-toolbar.

Another option is tu use the python manage.py shell to call those queries. You can use the django-extensions with the --print-sql parameter in order to display it.

Or, print the sql it self, I usally do this:

>>> qs = User.objects.filter(username='luan')
>>> print(qs.query)
SELECT "users_user"."id", "users_user"."password", "users_user"."last_login", "users_user"."is_superuser", "users_user"."username", "users_user"."first_name", "users_user"."last_name", "users_user"."email", "users_user"."is_staff", "users_user"."is_active", "users_user"."date_joined", "users_user"."ref_id", "users_user"."created_on", "users_user"."updated_on" FROM "users_user" WHERE "users_user"."username" = luan ORDER BY "users_user"."id" ASC

Upvotes: 1

Andrey Bondar
Andrey Bondar

Reputation: 130

You can try changing LOGGING config, for example in your settings.py (logs all your queries if you DEBUG is True):

LOGGING = {
    'version': 1,
    'filters': {
        'require_debug_true': {
            '()': 'django.utils.log.RequireDebugTrue',
        }
    },
    'handlers': {
        'console': {
            'level': 'DEBUG',
            'filters': ['require_debug_true'],
            'class': 'logging.StreamHandler',
        }
    },
    'loggers': {
        'django.db.backends': {
            'level': 'DEBUG',
            'handlers': ['console'],
        }
    }
}

Upvotes: 1

Related Questions