Here again you could make a SQL view with joins and make only a single select query to it.
Sometimes the orm grabs too much data from the database with many queries. We need to evaluate if we want to perform the filtering and association extraction in ruby world or if we can just do it in SQL world. On Jan 4, 2015 2:49 AM, "Jeremy Evans" <[email protected]> wrote: > On Saturday, January 3, 2015 2:27:04 PM UTC-8, metachronicler wrote: >> >> Does Sequel support sending multiple select statements in a single batch >> with a deferred execution mechanism such as the one detailed here: >> http://blog.andrewawhitaker.com/blog/2014/06/28/queryover-series-part-5- >> materializing-results/ and here:http://ayende.com/blog/ >> 3979/nhibernate-futures (Note that both are the same, one just uses a >> newer api than the other.)? >> > > Sequel doesn't support an API like this, each query is issued separately > to the database. I don't have current plans to add such an API. I'm not > sure how wide such support is across databases, and it would complicate the > adapter API (which is kept deliberately simple). > > >> >> The idea here is that you can defer the transmission of SQL to the >> database is such a way that only one call is ever made to the database even >> if you are retrieving multiple distinct datasets. This is extremely handy >> when trying to build complex objects with association fields and you want >> to pull in all of the child objects based on the parent table. Put another >> way given a set X sets A,B,C all use X in a subquery of the form: >> >> select * from (A, B, or C) >> where (A,B,or C).fk in(select X.pk from X) >> >> >> I'm using this kind of technique to reduce the number of SQL queries from >> over 2k to a value in double digits if not less at my job, with what I hope >> will remain a single trip to the database to grab everything. >> > > Sequel supports subselects, you can use a Sequel dataset anywhere you want > to use a subselect in SQL: > > ds = DB[:x].select(:x__pk) > [:a, :b, :c].map do |t| > DB[t].where(:fk=>ds).all > end > > While purely a guess in my part, I'm guessing if you want to optimize > this, it would be better to switch to using each instead of all, as opposed > to trying to send all queries at the same time, at least if any of the > results sets is large. > > Also, an alternative to the query you are proposing in Sequel would be to > use eager_loading: > > X.eager(:a, :b, :c).all > > Thanks, > Jeremy > > -- > You received this message because you are subscribed to the Google Groups > "sequel-talk" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/sequel-talk. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "sequel-talk" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/sequel-talk. For more options, visit https://groups.google.com/d/optout.
