#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.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/053.31a5c9941541c6e6dd8edd78a4d0468e%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.