Code Monkey home page Code Monkey logo

Comments (6)

chr1sjf0x avatar chr1sjf0x commented on June 3, 2024

Question: Is the main issue to be solved the fact that we currently do three round trips to get the results? Or that we logically want/need 1 sql query?

For instance, if we could issue these 3 queries simultaneously, wouldn't that be just as good? (and maybe better as duplication will be eliminated from the result)

SELECT * FROM author WHERE id = '1'
SELECT * FROM books WHERE author_id IN (1);  // No join needed, author_id is on books
SELECT * FROM book_reviews JOIN books ON books.id = book_reviews.book_id WHERE book.author_id in (1);

from joist-orm.

stephenh avatar stephenh commented on June 3, 2024

@chr1sjf0x ah yeah, in this instance it is to avoid 3 round trips; we can't issue the queries simultaneously b/c the IN in the 2nd query depends on the result of the 1st, and then IN in the 3rd query depends on the results of the 2nd.

...speaking of simultaneous queries, I have heard murmurs that PG itself has great support for "1 connection making multiple / simultaneous in-flight queries", but that the client driver libpq only just gained this functionality, and so the pg package we use may be artificially serializing queries.

I.e. for Joist, we could send UPDATE books ..., INSERT authors ..., UPDATE book_reviews ..., basically all of our mutations, essentially all simultaneously, but I believe/assume the driver is currently serializing them.

from joist-orm.

stephenh avatar stephenh commented on June 3, 2024

Filed #240

from joist-orm.

chr1sjf0x avatar chr1sjf0x commented on June 3, 2024

we can't issue the queries simultaneously b/c the IN in the 2nd query depends on the result of the 1st, and then IN in the 3rd query depends on the results of the 2nd.

Right, but what I'm getting at is it seems like it might be simpler to still have 3 queries, where each one joins what they need to get back to the value in the IN clause.

As opposed to combining the three into 1 large query?

from joist-orm.

stephenh avatar stephenh commented on June 3, 2024

Ah yes! It is definitely simpler, which is why it's been implemented that way so far. :-D

That said, if you meant "simpler" as in simplicity that means "the database can execute 3 simple-but-sequential queries faster than it can do 1 complex-with-joins query", unfortunately my suspicion is that that is not true, i.e. we are missing out on perf gains by doing the 1-query-with-joins approach (b/c even if the 1-complex-with-joins query takes 10ms instead of 5ms, it's still faster than 5ms+5ms+5ms if our cost is driven mostly by inherent per-call network latency).

With some disclaimers:

  1. there are probably pathological cases where the 3-join query returns significantly more data than separate queries (b/c it has to repeat the grandparent entity N times, repeat the parent entities 1 time per child, etc) such that 3 separate calls really is better (I assume this is rare...not sure...), and

  2. The "must do 1-query-with-joins" has been defacto ORM best practice for quite awhile, so maybe its out-of-date, b/c the response times of modern databases is just so amazing (e.g. the ORMs of the past didn't have sub-5ms Aurora response times, so it was much more important to avoid sequential network calls).

So I'd love for that to be the case 🤞 :-) but until really digging in/having numbers, I think it's a fair/almost sure assumption that we'd get net/net better perf by using the prototypical 1-query-with-joins approach to preloading.

from joist-orm.

stephenh avatar stephenh commented on June 3, 2024

This is implemented but not rolled out, until I can get a chance to have metrics in place that show improvements (better/worse/no change).

from joist-orm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.