marick

marick

Performance implications of Ecto within-query preloads with postgres

I know not to worry about performance before getting evidence of a problem, but I’m idly curious about preloads. (Note: I have fairly little experience with databases.)

I have this query:

#Ecto.Query<from a0 in Crit.Schemas.Animal, where: a0.id == ^"2",
 order_by: [asc: a0.name], distinct: [asc: a0.name],
 preload: [[:species, :service_gaps]]>

When that runs, the log shows three queries:

[debug] QUERY OK source="animals" db=0.5ms idle=1437.9ms
SELECT ... FROM "demo"."animals" WHERE (a0."id" = $1)  [2]

[debug] QUERY OK source="species" db=1.0ms idle=1438.7ms
SELECT ... FROM "demo"."species" ... WHERE (s0."id" = $1) [1]

[debug] QUERY OK source="service_gaps" db=1.5ms idle=1438.7ms
SELECT ... FROM "demo"."service_gaps" WHERE (s0."animal_id" = $1) [2]

Am I correct in thinking that’s three round trips to Postgres?

Most Liked

josevalim

josevalim

Creator of Elixir

Correct. Keep in mind that this is often the best way to load one to many associations because of the way SQL database return data. If you have three tables, all associated in one to many, and each table returns respectively K, M, N entries, you will get overall K * M * N rows. That ends up with a lot of duplication, which translates to more data over the wire and more work decoding the data. When using preloads, you get K + M + N rows, which ends up being more performant.

If you want to force to actually go the join route, you can do so too:

from q in query, join: s in assoc(q, :species), join: sg in assoc(q, :service_gaps), preload: [species: s, service_gaps: sg]
benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

It does it in 3 distinct queries yes, although notably if you aren’t in a transaction it will query the preloads in parallel.

Where Next?

Popular in Questions Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New

Other popular topics Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
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 49084 226
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

We're in Beta

About us Mission Statement