Hi, and thanks for making Django.

At the moment, when passing a RawQuerySet to an __in filter, the raw query 
is evaluated and the result passed as a parameter to the filter's SQL. It 
would be nice to have the option of using the raw SQL in a subquery 
instead, for performance reasons.

I think in this case the rationale for accepting 
https://code.djangoproject.com/ticket/14733 applies. Raw queries already 
require the developer to know what they're doing - this just makes them a 
little more useful.

I can see two ways of implementing this. The first is simple: just require 
that the query only select one column, and implement _as_sql on RawQuerySet 
to simply return the SQL and params. A flag passed to raw() could let a 
developer explicitly indicate that the query should be used as a subquery 
instead of evaluated.

The second way is more complicated but nicer: wrap the raw SQL in another 
SELECT, selecting the Model's primary key. The advantage of this method is 
that the raw query can select many columns, and therefore remain useful in 
other contexts, while still working as expected with __in. This could then 
be the default behaviour, with the current behaviour achieved by wrapping 
the query in list() as is the case for regular queries.

I much prefer the second approach, but I don't think I'm familiar enough 
with the ORM to implement it without a little guidance from someone more 
knowledgeable.

I'd love to hear some feedback on this idea!

Cheers,
Alex

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-developers/-/zfVpJLrs7XsJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-developers?hl=en.

Reply via email to