Guest User

Untitled

a guest
Apr 29th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.52 KB | None | 0 0
  1. from django.db import connections
  2. from django.db.models.query import QuerySet
  3. from __future__ import print_function
  4.  
  5. class QuerySetExplainMixin:
  6. def explain(self, analyze=True):
  7. cursor = connections[self.db].cursor()
  8. print(self.query)
  9. print()
  10. sql, params = self.query.sql_with_params()
  11. cursor.execute('EXPLAIN %s %s' % ("ANALYZE" if analyze else "", sql), params)
  12. map(lambda x: print(x[0]), cursor.fetchall())
  13. return None
  14.  
  15. QuerySet.__bases__ += (QuerySetExplainMixin,)
Add Comment
Please, Sign In to add comment