This is something I have been doing recently and was wondering if others do it too and if so what is the name for this type of practice.
I keep shortcut columns in my tables so I can avoid doing too many joins. For example if I have a users table and a geo table:
Users:
id | username | zip | copy_latitude | copy_longitude | other info
------------------------------------------------------------------------
1 | Bob | 11345 | 40.81518000 | -73.04550000 | etc...
Geo:
id | zip_code | latitude | longitude
----------------------------------------
1 | 11345 | 40.81518000 | -73.04550000
Now if I wanted to get Bob’s latitude and longitude I can do so in the same select statement that I would use to get bob’s other info:
SELECT a_bunch_of_other_columns, copy_latitude, copy_longitude
FROM users WHERE id = 1;
vs (if I didn’t keep the shortcuts):
SELECT a_bunch_of_other_columns, latitude, longitude
FROM users
INNER JOIN geo ON user.zip = geo.zip_code
WHERE users.id = 1;
By keeping the shortcuts I saved myself a join. Now this may not seem like a big deal for this example table and statement but I have some huge tables and statements with 6 or 7 joins and I feel like this helps.
The only overhead is having to update both places anytime something changes. I take care of that via stored procedures.
My questions are:
- is this a common practice among developers and if so what is it called?
- is my database still normalized if I do this? (I would assume so since I am always keeping a valid copy of the data in the proper
location even if I don’t use it from there, for the sake of data integrity)
It is not normalised anymore as you have duplicated data in your tables.
I guess you could call it “Denormalised”.
The only time your would really do it is for speed/optimisation purposes, which is what you are saying in your question, that you have done it to remove complexity.
Honestly I have never got to the point in any of my databases where I have needed to do this to optimise query speed.
I would suggest doing a benchmark to see just how much faster it is then a well indexed join