运行“解释”程序的简单方法在Django中的查询集 [英] Easy way to run "explain" on query sets in django

查看:100
本文介绍了运行“解释”程序的简单方法在Django中的查询集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Django中直接从查询集运行解释似乎应该很容易,但是我看不出如何做到这一点的任何明显方法,并且解释是很难在其中搜索到的文档。

It seems like it should be easy to run "explain" directly off of a queryset in Django, but I don't see anything obvious for how to do it, and "explain" is a difficult thing to search for in the docs.

推荐答案

好吧,除了工具栏外似乎什么也没有,所以我写了自己的mixin给我一个 explain()方法查询我的查询集:

Well, there seems to be nothing out there except a toolbar so I wrote my own mixin to give me an explain() method on my querysets:

from django.db import connections
from django.db.models.query import QuerySet

class QuerySetExplainMixin:
    def explain(self):
        cursor = connections[self.db].cursor()
        cursor.execute('explain %s' % str(self.query))
        return cursor.fetchall()

QuerySet.__bases__ += (QuerySetExplainMixin,)

希望这对其他人有用。

这篇关于运行“解释”程序的简单方法在Django中的查询集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆