[Django] #24638: New feature: support adding an SQL comment in queries

79 views
Skip to first unread message

Django

unread,
Apr 13, 2015, 5:12:09 PM4/13/15
to django-...@googlegroups.com
#24638: New feature: support adding an SQL comment in queries
-------------------------------------+-------------------------------------
Reporter: adamchainz | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 1.8
(models, ORM) | Keywords: queryset, query,
Severity: Normal | comment, optimizer hint
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
This feature gives QuerySet a `.comment(text)` method that inserts text
into a comment in the output SQL query, right after the `SELECT
[DISTINCT]` part. This has two uses:

1. For all database backends, this can be used for feature-to-query
tracking. For example, you might override `def queryset` on your admin
classes to use `comment()` to insert the name of the admin class
generating the query, making query analysis (processlist, slow query logs,
etc.) easier to track back to the point in the code generating the
queries. This is not trivial.

I've seen Facebook have nice tools that automatically insert file/line
number summaries into queries as comments to make analysis easy. Adding
`comment()` would making this easy for custom QuerySet subclasses to do
this however they want, with e.g. caller inspection at `__init__` time.

2. For the MySQL and Oracle backends, flags and optimizer hints can be
added. Both have a number of options for queries that are otherwise
unsupported on django, and they can be inserted with special comments.

This is my current motivation - I had a pretty complex 3-join/5-subquery
MySQL query today that could only be made good on the ORM by adding
MySQL's "STRAIGHT_JOIN" hint, and the only way I found to do this in
Django at current was to monkey-patch the MySQL backend CursorWrapper's
execute() to rewrite the generated query with regexes... :( And it
couldn't use raw() since it was being passed to the admin with the ability
to filter it.

I couldn't find any historical tickets on optimizer hints.

--
Ticket URL: <https://code.djangoproject.com/ticket/24638>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 13, 2015, 5:14:39 PM4/13/15
to django-...@googlegroups.com
#24638: New feature: support adding an SQL comment in queries
-------------------------------------+-------------------------------------
Reporter: adamchainz | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage:
comment, optimizer hint | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by adamchainz):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Ticket https://github.com/django/django/pull/4495

No documentation yet, don't know if it will be accepted...

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

Django

unread,
Apr 14, 2015, 9:57:37 AM4/14/15
to django-...@googlegroups.com
#24638: New feature: support adding an SQL comment in queries
-------------------------------------+-------------------------------------
Reporter: adamchainz | Owner: nobody
Type: New feature | Status: new

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage:
comment, optimizer hint | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by stevemcc):

* type: Uncategorized => New feature


--
Ticket URL: <https://code.djangoproject.com/ticket/24638#comment:2>

Django

unread,
Apr 14, 2015, 10:05:10 AM4/14/15
to django-...@googlegroups.com
#24638: New feature: support adding an SQL comment in queries
-------------------------------------+-------------------------------------
Reporter: adamchainz | Owner: nobody
Type: New feature | Status: new

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage: Accepted
comment, optimizer hint |

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by akaariai):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/24638#comment:3>

Django

unread,
Apr 14, 2015, 10:37:02 AM4/14/15
to django-...@googlegroups.com
#24638: New feature: support adding an SQL comment in queries
-------------------------------------+-------------------------------------
Reporter: adamchainz | Owner: nobody
Type: New feature | Status: new

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage: Accepted
comment, optimizer hint |
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* needs_docs: 0 => 1
* has_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/24638#comment:4>

Django

unread,
Apr 16, 2015, 5:22:19 PM4/16/15
to django-...@googlegroups.com
#24638: New feature: support adding an SQL comment in queries
-------------------------------------+-------------------------------------
Reporter: adamchainz | Owner: nobody
Type: New feature | Status: new

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage: Accepted
comment, optimizer hint |
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by adamchainz):

For reference (checking I did get the right place for Oracle and MySQL
query hints):

1. MySQL hints are listed in its syntax
https://dev.mysql.com/doc/refman/5.5/en/select.html (STRAIGHT_JOIN,
SQL_NO_CACHE, etc.). They can be inserted as comments by starting the
comment with an exclamation mark, which pretty much means "this is not a
comment" (the reason this exists is that if you follow the exclamation
mark with an encoded version number, that SQL will only be executed on
versions of the server at that version or above). Also newer more powerful
hints are coming in 5.7 with comments that start with a + :
https://dev.mysql.com/worklog/task/?id=8017

2. Oracle has extensive documentation on its hints which appear only in
conditional comments, although the page is lacking in examples:
http://docs.oracle.com/cd/B19306_01/server.102/b14211/hintsref.htm .
However the syntax for SELECT does show hints going *before* DISTINCT:
http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_10002.htm
, however this blog post shows them after: http://www.dba-
oracle.com/t_index_fast_full_scan.htm .

--
Ticket URL: <https://code.djangoproject.com/ticket/24638#comment:5>

Django

unread,
Apr 17, 2015, 1:02:46 AM4/17/15
to django-...@googlegroups.com
#24638: New feature: support adding an SQL comment in queries
-------------------------------------+-------------------------------------
Reporter: adamchainz | Owner: nobody
Type: New feature | Status: new

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage: Accepted
comment, optimizer hint |
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by akaariai):

It might be a bit ugly if we have to support multiple places for the
comment. What about MSSQL, DB2 and other non-core backends? I'm not sure
what is the best way to alter the generated SQL if we need support for
injecting hints in arbitrary places.

Also, are we ok with support for read queries only, or should we have it
for update/delete/insert queries, too? The delete queries might be
especially hard for cascades. Maybe we should aim for "user generated
select queries only" for the first implementation?

--
Ticket URL: <https://code.djangoproject.com/ticket/24638#comment:6>

Django

unread,
Apr 19, 2015, 5:57:33 AM4/19/15
to django-...@googlegroups.com
#24638: New feature: support adding an SQL comment in queries
-------------------------------------+-------------------------------------
Reporter: adamchainz | Owner: nobody
Type: New feature | Status: new

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage: Accepted
comment, optimizer hint |
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by adamchainz):

MSSQL - it appears all hints come in an OPTION clause after HAVING:
https://msdn.microsoft.com/en-us/library/ms189499.aspx
DB2 - it appears hints come in a %_HINTS clause after WHERE:
http://scn.sap.com/thread/1573275

So yes, unfortunately this is not general-purpose enough for adding query
hints.

I did try an implementation with the comments injected on
DeleteSQLCompiler but the cascade did lead to it pretty broken since the
query hint for delete was carried to the selects. User generated selects
only sounds good - the current patch does update too, but it's not often
you want to add hints on them anyway as there are no joins.

--
Ticket URL: <https://code.djangoproject.com/ticket/24638#comment:7>

Django

unread,
May 17, 2015, 11:50:13 AM5/17/15
to django-...@googlegroups.com
#24638: New feature: support adding an SQL comment in queries
-------------------------------------+-------------------------------------
Reporter: adamchainz | Owner: nobody
Type: New feature | Status: new

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage: Accepted
comment, optimizer hint |
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by adamchainz):

For the record, here's the first open-source version of the regex-based
SQL-rewriting code I have that adds MySQL's `STRAIGHT_JOIN`:
https://github.com/adamchainz/django-
mysql/commit/cf69c24962c4a22cc2fac5b29319777ee2a44fb0 . If we can't add
`.comment()`, this is the probably the best solution possible.

--
Ticket URL: <https://code.djangoproject.com/ticket/24638#comment:8>

Django

unread,
Jan 4, 2020, 4:32:50 AM1/4/20
to django-...@googlegroups.com
#24638: New feature: support adding an SQL comment in queries
-------------------------------------+-------------------------------------
Reporter: Adam (Chainz) | Owner: nobody
Johnson |
Type: New feature | Status: new

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage: Accepted
comment, optimizer hint |
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Susie Murray):

I want to know [https://tspiglobal.com/communication/who-invented-the-
internet/ who invented the internet]

--
Ticket URL: <https://code.djangoproject.com/ticket/24638#comment:9>

Django

unread,
May 18, 2022, 7:28:14 AM5/18/22
to django-...@googlegroups.com
#24638: New feature: support adding an SQL comment in queries
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Hannes
| Ljungberg
Type: New feature | Status: assigned

Component: Database layer | Version: 1.8
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage: Accepted
comment, optimizer hint |
Has patch: 1 | Needs documentation: 1
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hannes Ljungberg):

* owner: nobody => Hannes Ljungberg
* status: new => assigned

Django

unread,
May 18, 2022, 3:32:59 PM5/18/22
to django-...@googlegroups.com
#24638: New feature: support adding an SQL comment in queries
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: Hannes
| Ljungberg
Type: New feature | Status: assigned
Component: Database layer | Version: dev

(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage: Accepted
comment, optimizer hint |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hannes Ljungberg):

* needs_docs: 1 => 0
* version: 1.8 => dev


Comment:

PR: https://github.com/django/django/pull/15711

--
Ticket URL: <https://code.djangoproject.com/ticket/24638#comment:10>

Django

unread,
May 19, 2022, 3:15:29 AM5/19/22
to django-...@googlegroups.com
#24638: Support adding an SQL comment in queries
-------------------------------------+-------------------------------------

Reporter: Adam Johnson | Owner: Hannes
| Ljungberg
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage: Accepted
comment, optimizer hint |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/24638#comment:11>

Django

unread,
May 21, 2022, 5:22:16 AM5/21/22
to django-...@googlegroups.com
#24638: Support adding an SQL comment in queries
-------------------------------------+-------------------------------------

Reporter: Adam Johnson | Owner: Hannes
| Ljungberg
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage: Accepted
comment, optimizer hint |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hannes Ljungberg):

* needs_better_patch: 1 => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/24638#comment:12>

Django

unread,
May 25, 2022, 3:28:46 AM5/25/22
to django-...@googlegroups.com
#24638: Support adding an SQL comment in queries
-------------------------------------+-------------------------------------

Reporter: Adam Johnson | Owner: Hannes
| Ljungberg
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage: Accepted
comment, optimizer hint |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1

* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/24638#comment:13>

Django

unread,
Jan 25, 2023, 3:33:04 AM1/25/23
to django-...@googlegroups.com
#24638: Support adding an SQL comment in queries
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: (none)
Type: New feature | Status: new

Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage: Accepted
comment, optimizer hint |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: Hannes Ljungberg => (none)
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/24638#comment:14>

Django

unread,
Dec 19, 2023, 8:48:07 PM12/19/23
to django-...@googlegroups.com
#24638: Support adding an SQL comment in queries
-------------------------------------+-------------------------------------

Reporter: Adam Johnson | Owner: (none)
Type: New feature | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage: Accepted
comment, optimizer hint |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Michael Warkentin):

I've recently come across https://github.com/ossc-db/pg_hint_plan which
also provides support for plan hints in Postgres (which I'd like to use,
and the reason I've found this issue). Just thought that would be worth
considering as well during implementation if this work goes through.

--
Ticket URL: <https://code.djangoproject.com/ticket/24638#comment:15>

Django

unread,
Sep 1, 2025, 5:54:23 AMSep 1
to django-...@googlegroups.com
#24638: Support adding an SQL comment in queries
-------------------------------------+-------------------------------------
Reporter: Adam Johnson | Owner: (none)
Type: New feature | Status: new
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: queryset, query, | Triage Stage: Accepted
comment, optimizer hint |
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Petr Přikryl):

* cc: Petr Přikryl (added)

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