Owens
Hello all,
Does anyone know if you create a unique_index in postgres for two fields on a table, does that mean you DON’T need to create an additional index on those two tables?
e.g.
create unique_index(:projects, [:organization_id, :name])
means you DON’T need to do create index(:projects, [:organization_id])
create unique_index(:team_users, [:team_id, :user_id])
means you DON’T need to do
create index(:teams_users, [:team_id])
create index(:teams_users, [:user_id])
AND if you already setup those indexes before the unique_indexes, should you drop/delete those indexes before migrating the unique_indexes?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
It does not mean that. The index will only be used if you query both of those fields. If you query only one of them, it cannot use that index. If you plan to query the table by those fields individually then you also want indices on those fields.
Owens
Interesting, thanks for the answer.
Does that mean where you already have an index like
create index(:projects, [:organization_id])But you want to make sure that every project within an organization has a unique name, you should add a unique constraint instead of a unique index?
But with joins tables like :teams_users you should create a unique_index on the :team_id and :user_id rather than an index on each individually?
benwilson512
Unique constraints will create a unique index in the background anyway, it’s the only way to enforce the constraint in a performant manner.
Basically, create unique indices to enforce uniqueness rules. Also, create indices to reflect query patterns. If those two reasons to create indices happen to overlap, great! But it isn’t a big deal if they don’t, and they’re both perfectly valid reasons to create an index.
Owens
Thank you again, one follow-up question to this answer.
Unique constraints create a unique index in the background, does that mean one should also create a constraint to enforce uniqueness rules in addition to a unique index?
benwilson512
As far as I can tell there isn’t any practical difference, however I think from a clarity perspective it is better to use constraints to enforce uniqueness. Just know that you don’t need to make an index for it, one will be created already.
Owens
Thanks, and apologies as I think my last question may not make sense. I was referring to unique constraint within postgres and not in the Ecto schema.
But it looks to me like unique constraint like in the above style is not implemented in Ecto Migration and it just uses unique_index?
Thanks for all your help.
kip
With some material limitations, Postgres can use a leading subset of multi-column indexes. From the documentation:
But as Ben says, not appropriate to apply unique constraints.
benwilson512
Yup so was I, in fact ecto unique constraints rely on postgres constraints, they don’t replace them.
amnu3387
From what I understood from this (Multicolumn indexes pg 9.6 (but same up to 12) the indexes can be used when the query involves columns from the index, going from the left to right.
In this case the second index on
organization_iddoesn’t give you anything extra since the leftmost column of the unique index you created before, is that same column.Here you wouldn’t need a
team_idindex, but you would need auser_idif you are planning on querying theteams_userstable solely byuser_id(withoutteam_idthat is).The
team_idbeing the leftmost column declared on theunique_indexwould be used when a query containing ateam_idcolumn is made.Theoretically this applies to indexes with more than 2 columns too.
E.g.
unique index on a, b, cMeans that pg can use the index on a query containing:
aa,band
a,b,c(though it still goes through the planner ofc, so it might not use it, but that wouldn’t be because it can’t, at least this is my reading from the docs, it also means that the order in which you declare the columns for the index plays a role too)
sorentwo
As noted above, you can use one or more parts of a compound index, from the left to the right. It can be a fantastic space and performance saving tool. In fact, that’s why Oban only declares two indexes for all of the standard operations.
Here is the “biggest” one, which composes 5 fields:
https://github.com/sorentwo/oban/blob/master/lib/oban/migrations.ex#L417