MySQL – Is It OK to Duplicate Parent Field in Child Table for Performance?

database-designMySQLperformance

I have a very complicated query which involve more than 10 tables. Some parent tables are loaded only to search on one field. I wonder if I can copy this field in the child table (this data is never updated), so that I don't need to load the parent table in my query.

I've always learned that I should not store the same data in two different place, but maybe it's "allowed" for performance ?

For example, imagine a country table, a city table, and a street table. Having the country code in the street table would speed up some complicated queries.

Thanks

Best Answer

What you are doing is called denormalization.

The rule of thumb is "normalize till it hurts, denormalize till it works." Denormalization should follow the access pattern of your queries. If you have a common query that goes from joining on 10 tables to just a few with just a small bit of duplication, go for it!