New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Map access in PostgreSQL do not allow index usage #330
Comments
We will document it but perhaps, as per your recent comment in the mailing list, this should be reported to PGSQL too, because if we could do the rewrite, they certainly could too. :) |
I do not think that it would be so easy to do rewrite in this case. It is much easier to do in |
Couldn't
be rewritten to:
? In any case, I think our biggest issue is that we support both regular json and jsonb fields, and |
@josevalim that is why I think it could be done in
And, no, it couldn't as it wouldn't work for nested objects. There is no way to transform |
I don't think it is an issue to rewrite, after all, it is the job of the adapter to match on Elixir expressions and convert them to SQL. The important is to keep the semantics.
I don't think it is a concern, we will never have to parse $1. In our case, we always have a static list of keys, so we do have a good idea of the shape. The issue is that keys can be dynamic. So my previous example:
The complication is rather $1 needing to work as an integer or a string. |
I don't know if we have a precedent for different behaviour per server version, but fwiw pg 14 adds a subscript syntax:
(https://www.postgresql.org/about/news/postgresql-14-beta-1-released-2213/) and I'd expect it to be handled by indexes too which may or may not help here. |
Yes, I was telling if we would like to push that to the PostgreSQL planner. Planner/query builder would need to parse Of course, the arrays in JSON would be pretty hard, as @wojtekmach I am afraid it will not work either as per documentation it will work only if the operator is applied directly on column:
|
It also could be mitigated with better description how to declare indices on JSONB fields, as IIRC: create index(:foo, ["map #> '{val}'"]) Would allow usage of index in the above case. However I do not know if the DB would be smart enough to see that EDIT: Tested it, and |
@hauleth PR for the docs are always welcome! |
@hauleth Ping. :) |
@josevalim only one question - where it should be documented? In |
Environment
postgrex 0.15.9
Current behavior
Assuming that we have table like:
And query like:
Then the
foos_map_index
will not be used due to fact that Ecto will use#>
operator which is not part of GIN supported operator classes. This mean that this query will be less performant than less idiomatic:Expected behavior
This gotcha should be either documented or we should detect situations like above and translate them to the
@>
operator if possible (but that still can be surprising sometimes that it doesn't work as expected).The text was updated successfully, but these errors were encountered: