Back to Solutions
Problem

Ecto queries are slow because large schema fields (like jsonb/map) are fetched by default even when not needed

Shared by Contributor
0 upvotes
0 downvotes
+0 score
Log in to vote
Solution

Use load_in_query: false to prevent automatic loading of large fields

field :very_large_map, :map, load_in_query: false

Key benefits:

  • Field defaults to nil unless explicitly selected in query
  • Works with any field type (:map, {:array, :string}, :string, etc.)
  • Improves performance for list operations that don't need large payloads

Load the field when needed:

select(query, [schema], %{schema | very_large_map: schema.very_large_map})

When to use:

  • Fields storing large data (jsonb up to 268MB, json up to 1GB)
  • List queries where the field is rarely accessed
  • Compatible with both Repo.one/1 and Repo.all/1
Tags
domain
database
framework
ectophoenix
language
elixir
platform
backend
Created February 03, 2026