Last active
June 2, 2019 10:08
-
-
Save allixsenos/fc33744f14dc10cafd54 to your computer and use it in GitHub Desktop.
Django QuerySet Explain
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.db import connections | |
from django.db.models.query import QuerySet | |
from __future__ import print_function | |
class QuerySetExplainMixin: | |
def explain(self, analyze=True): | |
cursor = connections[self.db].cursor() | |
print(self.query) | |
print() | |
sql, params = self.query.sql_with_params() | |
cursor.execute('EXPLAIN %s %s' % ("ANALYZE" if analyze else "", sql), params) | |
map(lambda x: print(x[0]), cursor.fetchall()) | |
return None | |
QuerySet.__bases__ += (QuerySetExplainMixin,) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment