Debug queries
When building queries using the ORM, the end result can be inspected by calling to_sql()
:
The query will not be sent to the database, so it's safe to inspect all queries, no matter if they are performant or not.
Query plan
Visual inspection of the query is often not sufficient to understand query performance. For this purpose, databases like PostgreSQL provide
the EXPLAIN
functionality which, instead of executing the query, produces an execution plan:
When optimizing queries, this functionality is useful for finding queries that should be using indexes but perform a sequential scan instead.