[Django] #35294: Queryset explain truncated

16 views
Skip to first unread message

Django

unread,
Mar 12, 2024, 11:00:41 AM3/12/24
to django-...@googlegroups.com
#35294: Queryset explain truncated
-------------------------------------+-------------------------------------
Reporter: Gordon | Owner: nobody
Wrigley |
Type: | Status: new
Uncategorized |
Component: Database | Version: 4.2
layer (models, ORM) |
Severity: Normal | Keywords: explain
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Django 4.2.10
Python 3.10.13
Postgres 15.6
Psycopg2 2.9.9

I have some very complex querysets I'm trying to optimize and what I've
run into is explain output is being truncated at 100 lines. As far as I
can tell this is because of this function on
django.db.models.sql.compiler.SQLCompiler.

{{{#!python
def explain_query(self):
result = list(self.execute_sql())
# Some backends return 1 item tuples with strings, and others
return
# tuples with integers and strings. Flatten them out into strings.
format_ = self.query.explain_info.format
output_formatter = json.dumps if format_ and format_.lower() ==
"json" else str
for row in result[0]:
if not isinstance(row, str):
yield " ".join(output_formatter(c) for c in row)
else:
yield row
}}}

Where `result[0]` is ignoring additional results. Monkey patching it to

{{{#!python
def explain_query(self):
results = list(self.execute_sql())
# Some backends return 1 item tuples with strings, and others
return
# tuples with integers and strings. Flatten them out into strings.
format_ = self.query.explain_info.format
output_formatter = json.dumps if format_ and format_.lower() ==
"json" else str
for result in results:
for row in result:
if not isinstance(row, str):
yield " ".join(output_formatter(c) for c in row)
else:
yield row
}}}

Gets me the full explain output.
--
Ticket URL: <https://code.djangoproject.com/ticket/35294>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 12, 2024, 11:00:58 AM3/12/24
to django-...@googlegroups.com
#35294: Queryset explain truncated
-------------------------------------+-------------------------------------
Reporter: Gordon Wrigley | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: explain | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Gordon Wrigley):

* type: Uncategorized => Bug

--
Ticket URL: <https://code.djangoproject.com/ticket/35294#comment:1>

Django

unread,
Mar 12, 2024, 1:39:58 PM3/12/24
to django-...@googlegroups.com
#35294: Queryset explain truncated
-------------------------------------+-------------------------------------
Reporter: Gordon Wrigley | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: explain | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Mariusz Felisiak):

Interesting, do you observe the same with psycopg 3+?
--
Ticket URL: <https://code.djangoproject.com/ticket/35294#comment:2>

Django

unread,
Mar 12, 2024, 2:03:14 PM3/12/24
to django-...@googlegroups.com
#35294: Queryset explain truncated
-------------------------------------+-------------------------------------
Reporter: Gordon Wrigley | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 4.2
(models, ORM) |
Severity: Normal | Resolution:
Keywords: explain | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Comment (by Gordon Wrigley):

Yes.
--
Ticket URL: <https://code.djangoproject.com/ticket/35294#comment:3>
Reply all
Reply to author
Forward
0 new messages