What is the query that generated those results?
Ecto.Query.order_by/3
i.e. let the database do it - it’s good at it …
# file: music_db/priv/repo/playground.exs
#
# http://www.pragmaticprogrammer.com/titles/wmecto
# https://pragprog.com/titles/wmecto/source_code
# http://media.pragprog.com/titles/wmecto/code/wmecto-code.zip
#
# pg_ctl -D /usr/local/var/postgres start
# mix format ./priv/repo/playground.exs
# mix run ./priv/repo/playground.exs
#
defmodule Playground do
import Ecto.Query
alias MusicDB.Repo
alias MusicDB.{Album, Track}
def show(title, f) do
title
|> f.()
|> IO.inspect()
end
def play do
title = "Live At Montreaux"
show(title, &list_by_join/1)
show(title, &list_by_assoc/1)
show(title, &list_with_preload/1)
end
def list_by_join(title) do
from(a in Album,
inner_join: t in Track,
on: t.album_id == a.id,
where: a.title == ^title,
order_by: [asc: a.id, asc: t.index],
select: t
)
|> Repo.all()
end
def list_by_assoc(title) do
from(a in Album,
inner_join: t in assoc(a, :tracks),
where: a.title == ^title,
order_by: [asc: a.id, asc: t.index],
select: t
)
|> Repo.all()
end
def list_with_preload(title) do
from(a in Album,
inner_join: t in assoc(a, :tracks),
where: a.title == ^title,
preload: [tracks: t],
order_by: [asc: a.id, asc: t.index]
)
|> Repo.all()
end
end
Playground.play()
$ mix run ./priv/repo/playground.exs
09:59:15.617 [debug] QUERY OK source="albums" db=3.0ms
SELECT
t1."id",
t1."title",
t1."duration",
t1."index",
t1."number_of_plays",
t1."inserted_at",
t1."updated_at",
t1."album_id"
FROM "albums" AS a0
INNER JOIN "tracks" AS t1 ON t1."album_id" = a0."id"
WHERE (a0."title" = $1)
ORDER BY a0."id", t1."index"
["Live At Montreaux"]
[
%MusicDB.Track{
__meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
album: #Ecto.Association.NotLoaded<association :album is not loaded>,
album_id: 5,
duration: 761,
duration_string: nil,
id: 30,
index: 1,
inserted_at: ~N[2018-11-06 13:40:37.121579],
number_of_plays: 0,
title: "Anton's Ball",
updated_at: ~N[2018-11-06 13:40:37.121586]
},
%MusicDB.Track{
__meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
album: #Ecto.Association.NotLoaded<association :album is not loaded>,
album_id: 5,
duration: 647,
duration_string: nil,
id: 31,
index: 2,
inserted_at: ~N[2018-11-06 13:40:37.122448],
number_of_plays: 0,
title: "The Moontrane",
updated_at: ~N[2018-11-06 13:40:37.122455]
},
%MusicDB.Track{
__meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
album: #Ecto.Association.NotLoaded<association :album is not loaded>,
album_id: 5,
duration: 805,
duration_string: nil,
id: 32,
index: 3,
inserted_at: ~N[2018-11-06 13:40:37.123026],
number_of_plays: 0,
title: "Farallone",
updated_at: ~N[2018-11-06 13:40:37.123032]
},
%MusicDB.Track{
__meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
album: #Ecto.Association.NotLoaded<association :album is not loaded>,
album_id: 5,
duration: 844,
duration_string: nil,
id: 33,
index: 4,
inserted_at: ~N[2018-11-06 13:40:37.123612],
number_of_plays: 0,
title: "Song Of Songs",
updated_at: ~N[2018-11-06 13:40:37.123617]
}
]
09:59:15.630 [debug] QUERY OK source="albums" db=2.5ms
SELECT
t1."id",
t1."title",
t1."duration",
t1."index",
t1."number_of_plays",
t1."inserted_at",
t1."updated_at",
t1."album_id"
FROM "albums" AS a0
INNER JOIN "tracks" AS t1 ON t1."album_id" = a0."id"
WHERE (a0."title" = $1)
ORDER BY a0."id", t1."index"
["Live At Montreaux"]
[
%MusicDB.Track{
__meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
album: #Ecto.Association.NotLoaded<association :album is not loaded>,
album_id: 5,
duration: 761,
duration_string: nil,
id: 30,
index: 1,
inserted_at: ~N[2018-11-06 13:40:37.121579],
number_of_plays: 0,
title: "Anton's Ball",
updated_at: ~N[2018-11-06 13:40:37.121586]
},
%MusicDB.Track{
__meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
album: #Ecto.Association.NotLoaded<association :album is not loaded>,
album_id: 5,
duration: 647,
duration_string: nil,
id: 31,
index: 2,
inserted_at: ~N[2018-11-06 13:40:37.122448],
number_of_plays: 0,
title: "The Moontrane",
updated_at: ~N[2018-11-06 13:40:37.122455]
},
%MusicDB.Track{
__meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
album: #Ecto.Association.NotLoaded<association :album is not loaded>,
album_id: 5,
duration: 805,
duration_string: nil,
id: 32,
index: 3,
inserted_at: ~N[2018-11-06 13:40:37.123026],
number_of_plays: 0,
title: "Farallone",
updated_at: ~N[2018-11-06 13:40:37.123032]
},
%MusicDB.Track{
__meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
album: #Ecto.Association.NotLoaded<association :album is not loaded>,
album_id: 5,
duration: 844,
duration_string: nil,
id: 33,
index: 4,
inserted_at: ~N[2018-11-06 13:40:37.123612],
number_of_plays: 0,
title: "Song Of Songs",
updated_at: ~N[2018-11-06 13:40:37.123617]
}
]
09:59:15.632 [debug] QUERY OK source="albums" db=1.6ms
SELECT
a0."id",
a0."title",
a0."inserted_at",
a0."updated_at",
a0."artist_id",
t1."id",
t1."title",
t1."duration",
t1."index",
t1."number_of_plays",
t1."inserted_at",
t1."updated_at",
t1."album_id"
FROM "albums" AS a0
INNER JOIN "tracks" AS t1 ON t1."album_id" = a0."id"
WHERE
(a0."title" = $1)
ORDER BY a0."id", t1."index"
["Live At Montreaux"]
[
%MusicDB.Album{
__meta__: #Ecto.Schema.Metadata<:loaded, "albums">,
artist: #Ecto.Association.NotLoaded<association :artist is not loaded>,
artist_id: 3,
genres: #Ecto.Association.NotLoaded<association :genres is not loaded>,
id: 5,
inserted_at: ~N[2018-11-06 13:40:37.118081],
title: "Live At Montreaux",
tracks: [
%MusicDB.Track{
__meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
album: #Ecto.Association.NotLoaded<association :album is not loaded>,
album_id: 5,
duration: 761,
duration_string: nil,
id: 30,
index: 1,
inserted_at: ~N[2018-11-06 13:40:37.121579],
number_of_plays: 0,
title: "Anton's Ball",
updated_at: ~N[2018-11-06 13:40:37.121586]
},
%MusicDB.Track{
__meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
album: #Ecto.Association.NotLoaded<association :album is not loaded>,
album_id: 5,
duration: 647,
duration_string: nil,
id: 31,
index: 2,
inserted_at: ~N[2018-11-06 13:40:37.122448],
number_of_plays: 0,
title: "The Moontrane",
updated_at: ~N[2018-11-06 13:40:37.122455]
},
%MusicDB.Track{
__meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
album: #Ecto.Association.NotLoaded<association :album is not loaded>,
album_id: 5,
duration: 805,
duration_string: nil,
id: 32,
index: 3,
inserted_at: ~N[2018-11-06 13:40:37.123026],
number_of_plays: 0,
title: "Farallone",
updated_at: ~N[2018-11-06 13:40:37.123032]
},
%MusicDB.Track{
__meta__: #Ecto.Schema.Metadata<:loaded, "tracks">,
album: #Ecto.Association.NotLoaded<association :album is not loaded>,
album_id: 5,
duration: 844,
duration_string: nil,
id: 33,
index: 4,
inserted_at: ~N[2018-11-06 13:40:37.123612],
number_of_plays: 0,
title: "Song Of Songs",
updated_at: ~N[2018-11-06 13:40:37.123617]
}
],
updated_at: ~N[2018-11-06 13:40:37.118088]
}
]
$