﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
24638	Support adding an SQL comment in queries	Adam Johnson	Sidharth Dusanapudi	"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."	New feature	assigned	Database layer (models, ORM)	dev	Normal		queryset, query, comment, optimizer hint	Petr Přikryl	Accepted	1	0	0	1	0	0
