The concept of batched SELECT statements doesn't really exist in SQL, 
unless the relations you're selecting have identical column types. Example:

SELECT name, age_in_years FROM person
UNION ALL
SELECT item_name, quantity FROM item;

The UNION here means combine the results of each query into the one result 
set. A query like this probably isn't useful though, because you have no 
way of knowing which row belongs to which relation (or model). UNION 
queries are useful for specific kinds of queries, not as a general purpose 
batch method.

Can you see the issue with trying to batch queries that aren't exactly the 
same?

SELECT a, b FROM T1
UNION ALL
SELECT d, e, f from T2;

What would the result set look like if this was possible?

Your example of session and user could be made to use a single query if the 
session had a foreign key to the user table. It doesn't though, because you 
don't *need* users for sessions to work. This is why django has to parse 
the session data to determine whether or not it has to load a user object.

To answer your questions, no, django doesn't let you batch up multiple 
select statements. It can't, because that's not how SQL works. Connection 
pooling should help reduce the initialisation time of creating the 
connection, and multiple select statements can be sent over that single 
connection though, which is the closest you're going to get.

Regards,

On Friday, 27 February 2015 23:14:05 UTC+11, Ram Rachum wrote:
>
> Hi guys,
>
> After asking this question on django-users:
>
> https://groups.google.com/forum/#!topic/django-users/EuPduHjSNos
>
> And in several other forums, and not finding a solution, I've reached a 
> conclusion: It would be really helpful to allow batching SQL queries in 
> Django. I should preface by saying I'm not very good at SQL and I don't 
> know how the ORM works exactly, so maybe what I'm saying is wrong in some 
> way, if so please correct me.
>
> I know Django already support bulk insertion, but what I want is to make 
> multiple SQL reads, of various kinds, and have them populate multiple model 
> instances. This is important for lowering the number of database roundtrips 
> that Django does.
>
> I gave one example for when this need arises in the above link; another 
> example is how on every request, Django fetches the session, it then parses 
> the session, looks for the user ID, and then fetches the user. This is a 
> waste. If Django could do a combined query that fetches both the session 
> and the user in SQL, this would be best. I'm not good at SQL, so I'm not 
> sure whether it can be done. But that's just one example of where you can 
> save a roundtrip to the database.
>
> Am I right that Django doesn't currently let you do that? Do you think 
> it's possible to make Django do that? 
>
>
> Thanks,
> Ram.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers  (Contributions to Django itself)" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/a888777b-de4c-4ef6-b351-afc620601f64%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to