Normalization is from the devil
ayende.com
ayende.com
Umm, not really. Two words: referential integrity.
Every un-normalized database I've inherited has been a colossal mess and practically unusable -- especially if there are important relations. A de-normalized database is a thoughtful process which I rarely see a developer do -- they just want to get programming and don't think about long-term issues.
All they've done is remove the connection between Products and OrderLines, which is partially rational in the real world: what if you want to give a discount? A partial refund? What if your products change prices at times? If you don't include the cost on the OrderLine, you've gotta handle version issues for every single instance.
As to the product name... why would it change? A new company took over the product? I'll bet it has a different cost, new packaging, and a new description. Is it really the same product? And, by disconnecting them, they've now lost any ability to do comparative analysis, recommendations, num-sold... anything meaningful aside from displaying the order.
If your only goal is display, just save the rendered webpage you presented them. It has as much meaning, and will always look the same. If it's doing anything with your data, normalize away.
Pretty trivial, and loads all the needed records in a single query.
> This will load all comments with a single query. This reduces the total number of queries to 3. More generally the number of queries will be 1 plus the number of associations named (except if some of the associations are polymorphic belongs_to - see below).
Good call. Still, the difference between 1 and 3 queries is nothing compared to 3 and 100s (n+1 problem).
I wonder what AR can't turn that into a single query.