Skip to content


Rails and inner query choices

Consider that we have two models; Well and SurveyPoint . A Well represent an oil/gas/water well, each with multiple SurveyPoints. A SurveyPoint represent the well’s coordinate at a certain depth.

Say, we’d like to implement a filtration mechanism that allows us to query the wells – and for those set of wells we’d like to conditionally present the set of survey_points for those set of filtered wells.

A method ‘ Well.search‘ would returns the set of wells we’re interested in ( @wells )

Using the power of AREL chaining, we could then conditionally extract the SurveyPoints using the following:

This roughly translates into the query:

The challenge here is that if both results is large, depending on the cardinality – the database engine might not be able to make use of defined indexes and the outer query might not be optimizable.

Instead – if we use pluck instead of select:

Rails would fire two queries instead of an inner one. Pluck would first extract the list of ids (val_1, val_2, …) in an array before passing it to the main query. This roughly translates into:

No inner query is used in that case and the main one is easy to optimize.

Posted in Ruby.

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

(required)

(required, but never shared)

or, reply to this post via trackback.