@csrf_exempt
def sql_explain(request):
    form = SQLSelectForm(request.POST or None)

    if form.is_valid():
        sql = form.cleaned_data['raw_sql']
        params = form.cleaned_data['params']
        vendor = form.connection.vendor
        cursor = form.cursor

        if vendor == 'sqlite':
            # SQLite's EXPLAIN dumps the low-level opcodes generated for a query;
            # EXPLAIN QUERY PLAN dumps a more human-readable summary
            # See http://www.sqlite.org/lang_explain.html for details
            cursor.execute("EXPLAIN QUERY PLAN %s" % (sql,), params)
        elif vendor == 'postgresql':
            cursor.execute("EXPLAIN ANALYZE %s" % (sql,), params)
        else:
            cursor.execute("EXPLAIN %s" % (sql,), params)

        headers = [d[0] for d in cursor.description]
        result = cursor.fetchall()
        cursor.close()
        context = {
            'result': result,
            'sql': form.reformat_sql(),
            'duration': form.cleaned_data['duration'],
            'headers': headers,
            'alias': form.cleaned_data['alias'],
        }
        # Using render_to_response avoids running global context processors.
        return render_to_response('debug_toolbar/panels/sql_explain.html', context)
    return HttpResponseBadRequest('Form errors')
Returns the output of the SQL EXPLAIN on the given query
dataset/ETHPy150Open django-debug-toolbar/django-debug-toolbar/debug_toolbar/panels/sql/views.py/sql_explain