hauleth

hauleth

[PSA] Ecto.Query JSON-access API cannot use PostgreSQL GIN indices

If you have table like:

create table(:foos) do
  add :data, :json
end

create index(:foos, [:data], using: :gin])

Then queries that use json_extract_path/2, or equivalent Access-like syntax, will not utilise that index. In other words:

from foo in Foo,
  where: foo.data["bar"] == "yay"

Will require full table scan, and can kill performance of your queries.

There are 2 solutions to this problem:

  • If you know the subset of queried fields beforehand, then you can create functional index on these fields, for example create index(:foos, ["(data#>'{bar}'"])
  • If you do not know the subset of fields and want to run queries on arbitrary path, then instead you should use fragment/1 with @> operator, so in this case it would be from foo in Foo, where: fragment("? @> ?", foo.data, ~S({"bar": "yay"}})). Beware that this have different behaviour when working with arrays.

https://github.com/elixir-ecto/ecto/pull/3698

https://github.com/elixir-ecto/ecto_sql/issues/330

Where Next?

Popular in Discussions Top

CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 20070 166
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
slashdotdash
Phoenix Live View is now publicly available on GitHub. Here’s Chris McCord’s tweet announcing making it public.
New
lucaong
Hello Elixir and Nerves community, I have been working for a while on an open-source embedded key-value database for Elixir, that I call...
230 14362 124
New
tomekowal
Hey guys! I want to create a toy project that shows a chart of temperature over time and updates every 5 seconds. I feel LiveView is per...
New
matthias_toepp
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold. Gleam, alon...
New

Other popular topics Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

We're in Beta

About us Mission Statement