hudsonbay
Hi, please. I have a question regarding migrations in Ecto. But maybe this more of a PostgreSQL question rather than an Elixir one.
I have a many-to-many relation between students and teachers. One student can have many teachers and the same teacher can have many students.
So, I’m basically defining my migration like this:
def change do
create table(:students_teachers, primary_key: false) do
add :id, :binary_id, primary_key: true
add :criteria, :integer, null: false
add(:student_id, references(:students, on_delete: :delete_all, type: :binary_id),
null: false
)
add(:teacher_id, references(:teachers, on_delete: :delete_all, type: :binary_id),
null: false
)
end
create index(:students_teachers, [:student_id])
create index(:students_teachers, [:teacher_id])
end
Ok, my doubt is about the last line of the code, the part where I define the indexes:
create index(:students_teachers, [:student_id])
create index(:students_teachers, [:teacher_id])
But if I do it this in a second way create index(:students_teachers, [:student_id, :teacher_id]) something is gonna change.
If I do a diff to the two \d students_teachers generated tables in PostgreSQL with the different types of migrations I will see that there are some changes.
This the output of my diff (which BTW only sees the difference in the index definition):
> "students_teachers_student_id_index" btree (student_id)
> "students_teachers_teacher_id_index" btree (teacher_id)
---
< "students_teachers_student_id_teacher_id" btree (student_id, teacher_id)
So, my question is:
How do this two approaches change the way my data is related? What is the difference here? How do the teacher and the student are related according to the different ways I migrated the database?
Trending in Questions
Other Trending Topics
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
- #ecto-query
- #elixirconf-us
- #blog-post
- #ai
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
cenotaph
Individual indexes on
student_idandteacher_idfieldsSELECT ..... WHERE student_id=1 => FAST SELECTSELECT ..... WHERE teacher_id=1 => FAST SELECTYou would be telling Ecto to create a
composite indexSELECT ..... WHERE student_id=1 => SLOW SELECTSELECT ..... WHERE teacher_id=1 => SLOW SELECTSELECT ..... WHERE student_id= AND teacher_id=1 => FAST SELECTyurko
The first two queries are not that bad either, they’d still use indexes, especially the first one. Here’s some info about that:
hudsonbay
Yes, but than can also be solved if I pass the
:idof the table (students_teachers_id). Then, by usingRepo.preloadI can have the student’s data and the teacher’s data, right?. With that said, I still have doubts on what on what is the best thing to do regarding on what type of index to use (:hudsonbay
So what you are saying is that if I use this
create index(:students_teachers, [:student_id, :teacher_id])I can have this:?
cenotaph
Let’s focus on your SQL question first
Rule of thumb:
teacher_idORstudent_idyou should create two indexes individually. I assume this is 99% the use case for most tables.ALWAYSfetch the records from that table byteacher_idANDstudent_id, you should create a composite index.Nope, that was not my statement. My statement was
SLOW SELECTon top 2.For clarification, when I say
SLOWit is relative to correct index scan. SQL storages are fast enough to make up for the missing time, it is not easy for us humans to grasp the gap of milliseconds, microseconds, nanoseconds.But that nano, milliseconds add up to minutes, hours, days over millions, billions of queries, transactions.
Repo.preloadis all related to your definition of relationships. As long as there is aforeign_key(relation) path for Ecto to figure out the relationship, it will load them for you, regardless of howslow or inefficientthe relationship is.hudsonbay
Ok, I get it. thanks
yurko
If you want to simplify the answer and make it slow / fast, then with your index (student_id, teacher_id) it would be
See the link in my above comment for more on the topic of how slow the second query would be (not that slow).
From the Postgres docs (PostgreSQL: Documentation: 9.6: Multicolumn Indexes):