threads # list of structs
|> MyApp.Repo.preload(messages: {MyApp.Chat.recent_messages_in_thread(), [:author]})
defmodule MyApp.Chat do
def recent_messages_in_thread(query \\ MyApp.Chat.Message) do
from messages in query,
distinct: messages.thread_id,
order_by: [asc: messages.thread_id, desc: messages.inserted_at]
end
end
Unless you’re joining data in the query preloading will do additional queries as well. You could join messages only for your first chat, but this would need you knowing which chat the first one is or window functions.
Preloading is the act of loading associations for ecto schemas. This can happen in multiple ways, but the default is that associations are loaded in separate queries. So no more performant than a manual query for the messages of your first chat.
The only thing potentially making preloads more performant is joining the needed data in the query, which loads the primary schema and the assoc in one query. Joining messages would by default join them for all chats, but you can alter the join condition to join messages only for the chat id of the first chat and not for all other chats. Preloading that joined binding would mean all chats having an list of messages preloaded, but only the first one would actually hold messages, while all others would be empty lists.
So here we should make sure not to take joining data and preloading for the same thing.