What do you mean by a single roundtrip?

You can do multiple queries in a single transaction (thus in a single connection to
the DB). By default (correct me if I'm wrong) with PG for example, it does
all the queries in a single transaction while being in a request.


On 02/27/2015 03:18 PM, Ram Rachum wrote:

Thank you for the explanation. I still wonder though: you asked what the result set would look like, given it's two different tables. What I want is multiple result sets, but in only one roundtrip. Is it possible to send multiple SQL queries to the database in one roundtrip? We'd need the second query to use information from the first for the session usecase, but even if we can't use the information from the first query, it'll still be useful for other cases.

Is it possible to send multiple queries in a single roundtrip?

Sent from my phone.

On Feb 27, 2015 4:13 PM, "Josh Smeaton" <josh.smea...@gmail.com <mailto:josh.smea...@gmail.com>> wrote:

    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
        <https://groups.google.com/forum/#%21topic/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 a topic in
    the Google Groups "Django developers (Contributions to Django
    itself)" group.
    To unsubscribe from this topic, visit
    https://groups.google.com/d/topic/django-developers/3Lk-HEF16iI/unsubscribe.
    To unsubscribe from this group and all its topics, send an email
    to django-developers+unsubscr...@googlegroups.com
    <mailto:django-developers+unsubscr...@googlegroups.com>.
    To post to this group, send email to
    django-developers@googlegroups.com
    <mailto: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
    
<https://groups.google.com/d/msgid/django-developers/a888777b-de4c-4ef6-b351-afc620601f64%40googlegroups.com?utm_medium=email&utm_source=footer>.
    For more options, visit https://groups.google.com/d/optout.

--
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 <mailto:django-developers+unsubscr...@googlegroups.com>. To post to this group, send email to django-developers@googlegroups.com <mailto: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/CANXboVYYSWwq7_P2LfA2-XoxptB0U7YcO7o%3DAKKCRxcJTLXvEA%40mail.gmail.com <https://groups.google.com/d/msgid/django-developers/CANXboVYYSWwq7_P2LfA2-XoxptB0U7YcO7o%3DAKKCRxcJTLXvEA%40mail.gmail.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
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/54F08B1E.6000300%40arkade.info.
For more options, visit https://groups.google.com/d/optout.

Reply via email to