The title doesn't make too much sense, but I couldn't think a better title for this problem.
I have the following tables
Projects
- id
- name
Customers
- id
- id_project
- name
Payments
- id
- id_customer
- date
- sum
When a users enters the system, he will have access to a certain project. Now, I want to list all the payments for that project, and it should be pretty easy:
SELECT FROM payments where id_customer in (SELECT id from customers where id_project = 5)
My question is : if it isn't better to add a column id_project to payments table this way the queries will be easier and faster.
Best Answer
It seems you are asking if denormalization makes sense.
The answer is always "it depends", so here's my rule of thumb:
If ...
then stay normalized. Yes, denormalization is faster, but it also means you have redundant data in the system -- data that has to be maintained and kept in sync. There is no longer "one source" for that data, but multiple sources that can deviate. This is risky over time, so you shouldn't do it unless you have very good reasons to do it, backed by some benchmarks.
I would only denormalize when ...
Joins are very fast on modern hardware, but they are never free.