Markus Johansson
Markus Johansson

Reputation: 3773

How to get number of SQL queries made?

I have an sqlalchemy application. Some operations result in many SQL queries being made. Is there a way to get information from sqlalchemy of how many SQL queries have been made during the session?

I would like to log this, together with other information about the operation so that I later can go through the log and find operations which have too many queries made.

Upvotes: 0

Views: 104

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124100

SQLAlchemy logs all queries with the logging module, but you'll need to configure your logging setup to increase the logging level to logging.INFO to see the queries:

import logging

logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)

See the Configuring Logging documentation, as well as the logging module documentation. It is perfectly possible to redirect all sqlalchemy.engine output to a separate destination, for example, to analyze what queries are run more often.

Upvotes: 1

Related Questions