Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the raw SQL of the query being executed by SQL Alchemy

we are using SQL alchemy as the ORM for our project. i am trying to get & analyze the raw SQL that gets generated (via SQL alchemy) for performance purposes. can somebody please let me know if there is a way to gather the raw SQL before it gets committed to the database.

like image 940
Tanu Avatar asked Oct 16 '25 13:10

Tanu


1 Answers

You can set echo=True when instantiating your engine:

>>> from sqlalchemy import create_engine
>>> engine = create_engine("mysql://localhost/mydb", echo=True)

This causes all SQL statements to be logged via normal Python logging. You can easily capture these messages to a file by using the appropriate log configuration, as shown in these Python logging examples.

Reference: SQLAlchemy Engine Configuration

like image 123
Alex Smith Avatar answered Oct 19 '25 02:10

Alex Smith