First of all, I think making the auto-prefetch feature available in some form
is a great idea. As an opt-in, I would have made use of it on several
occasions, and cut days of optimization work to minutes. It's true that this
wouldn't achieve the best possible optimization in many cases, but it would be
close enough, for a small fraction of the effort.
But of the choices that have been suggested here, I find none quite
satisfactory. I agree, basically, with almost all of the objections raised on
all sides. And I think I have a better suggestion.
Rather than a setting (global and essentially constant) or all-local opt-ins,
suppose the feature was controlled -- for all querysets -- by a context
manager.
So, rather than
> "qs.prefetch_related(auto=True)", or "with qs.auto_prefetch():",
suppose we had
with QuerySet.auto_prefetch(active=True):
and under that, all querysets would auto-prefetch. Then:
- We could put that in a middleware; using that middleware would then,
effectively, be a global opt in. We could add the middleware -- perhaps
commented-out -- to the new project template, for beginner's sake. Call it
PerformanceTrainingWheelsMiddleware, to make people aware that it's something
they should grow out of, but make it easily accessible.
- We could easily build a decorator for views
- We could have local opt-out, at the correct level -- not the objects, but
the control flow.
For this to work, the flag controlling the feature would need to be thread-
local and managed as a stack. But these aren't problems.
If (as likely) this suggestion still does not generate a concensus, I would
prefer that we follow the path Anssi suggested --
> Note that it should be possible to implement this fully as 3rd party
> project if we add an easy way to customise how related object fetching is
> done. I'm not sure if we can add an easy way for that.
Except I don't think "easy" is a requirement here. If we can add a sensible
way, that would be enough -- I don't expect many Django users to develop
implementations of the feature, only to use them.
My 2 cents,
Shai.