njwest

njwest

Ecto Join/Select Database Efficiency Questions

So I am using the Ecto/PSQL join/select query at the end of this post to get select child and parent data to show through a feed.

I am then matching the selected variables in my html.eex template with elem(child_parent_data, 1).

Two questions:

  1. Is this the most efficient way to get this joined/selected data, by selecting it and feeding object through elem(object, index)?

  2. Do you think pattern matching elem(object, index) to a map ( %{"parent_logo" => elem(object, 0), "parent_name" => ...} etc. ) then feeding the map to a template would be more expensive or less expensive than just passing elem(object, index)s directly in the template?

The query:

def get_children_with_parents do
    query = from c in Child,
      where: c.is_active == true,
      join: p in Parents, where: p.id == c.parent_id,
      select: { 
         p.logo, 
         p.name,
         p.desc,
         c.name,
         c.image,
         c.type,
         c.slug,
         c.id
     }

Most Liked

darkbaby123

darkbaby123

I don’t think there is a performance difference between map and tuple. But tuple, especially long tuple, is hard to remember the position of each element. So I rarely use tuples for database results.

For better maintainability, why not create relationship and use preload like this:

query = from(Child, where: [is_active: true], preload: [:parent])
children = Repo.all(query)

Enum.each(children, fn child ->
  child.name
  child.parent.logo
end)

If you have to use inner join (i.e. some child has no parent), then:

from(
  c in Child,
  join: p in assoc(c, :parent),
  where: [is_active: true]
  preload: [parent: p]
)

You can also use select to reduce the fields needed. The key point is that using struct/map is much clear than tuple.

mbuhot

mbuhot

Ecto supports selecting into a map directly. Not sure if it has any measurable performance impact vs tuples, but it is certainly more convenient than remembering the order of elements in a tuple :slightly_smiling_face:

jfeng

jfeng

For the pattern matching, since you have a fixed tuple coming back why not just pattern match the tuple?

{
  parent_logo,
  parent_name,
  parent_desc,
  child_name,
  child_image,
  child_type,
  child_slug,
  child_id
} = object

Alternately, if you can modify the database, you could create a view for that query and create an Ecto schema over the view.

Where Next?

Popular in Questions Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
shahryarjb
Hello, I get Persian date from my client and convert it to normal calendar like this: def jalali_string_to_miladi_english_number(persi...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement