I slept in school on database lesson and it sounded like nonsense anyway what they did in Microsoft Access. In MySQL everything is nice and clear. However i kind of puzzled by how to link tables together.
I currently use this style
primary table users primary key user_id other keys irrelevant
table documents primary key document_id second column user_id (from user table)
table schedule primary key document_id second column user_id (from user table)
table interview primary key document_id second column user_id (from user table)
and so on 20 more tables that connect between each other by user_id.
and in pages i do one query where i join all tables. sometimes 10 tables or 20 tables join like below without any speed problem takes on average about 1 second for query to run. Tip for speed - to have index in other tables based for the column you use in LEFT JOIN criteria or be numeric.
SELECT users.first_name,
documents.document_date,
interview.interview_result,
...
FROM users
LEFT JOIN documents ON documents.user_id = users.user_id
LEFT JOIN interview ON documents.user_id = users.user_id
...
LIMIT 0, 20
I know some people code like 5 different queries either sequential (one query selects data used in another query e.g. with loop then data from second query used in third query condition trough loop or smth) or independently select data from 5 tables and then in PHP join it all together in array based on user id.
Also inside tables i have several records per user e.g. in "documents" i have several records per user for each document we need from him they differ by "document_type" field.
However i had to do a page where i get all data for user from each table so query was not too practical and slow as hell to execute.
SELECT users.first_name,
doc_resume.document_date AS resume_date,
doc_passport.document_date AS passport_date,
doc_photo.document_date AS photo_date,
interview.interview_result,
...
FROM users
LEFT JOIN documents AS doc_resume ON doc_resume.user_id = users.user_id WHERE doc_resume.document_type = 1
LEFT JOIN documents AS doc_passport ON doc_passport.user_id = users.user_id WHERE doc_passport.document_type = 2
LEFT JOIN documents AS doc_photo ON doc_photo.user_id = users.user_id WHERE doc_photo.document_type = 3
LEFT JOIN interview ON documents.user_id = users.user_id
...
LIMIT 0, 20
Now i might need to design system where i have to get all data for user all the time, so obviously this approach of selecting with one query is not the best in this situation.
NOW THE QUESTION:
I rather have to select independently and then join in PHP or there's way to organize database so i can do it all in one query?
I was thinking maybe if i DON'T put user_id in each table and rather have one table called "link_table" where i have primary key "link_id" (not used), second column user_id, third column document_id (key from documents table) fourth column interview_id (key from interview table)
But then after writing this question i realized that in link table i will still have many records per each user e.g. for each record in documents i will have record link_table in link table, so it actually does not solve anything. or am i wrong.
Edit: Actually i wanted to know not about speed, but rather about should i use dedicated table for linking tables together or not is there any other advantages/disadvantages than speed?