开发者

normalize or not?

开发者 https://www.devze.com 2023-03-10 21:16 出处:网络
I have a DB in which there are 4 tables. A -> B -> C -> D Current the way I have it is, the Primary Key of A is a foreign key in B. And B would have it\'s own Primary Key, which is a foreign key in

I have a DB in which there are 4 tables.

A -> B -> C -> D

Current the way I have it is, the Primary Key of A is a foreign key in B. And B would have it's own Primary Key, which is a foreign key in C, etc etc.

However, C can't be linked to A without B.

The problem is, a core function of my program involve pulling matching entries from A and D.

Should I include the primary key of A开发者_如何学C in D too

Doing so will create unnecessary data duplication 'coz A->B->C->D are hierarchy. see pic for what D would look like.

normalize or not?


If you take all D-s in relation with given A, I would keep it normalized.

But if you want specific subset of such D-s and its easy to know which in advance, but time consuming later (eg. if you want all D-s from newest C from newest B), I would prefare storing this shortcut somewhere.

It does not have to be in D itself (esp. if you don't want all D-s connected with A).

If you want to do it to make your queries easier to read and write, then consider view.

If you want to do it to increase performance, try everything and measure it. (And I'm not expert in performance tuning of SQL, so I have no specific advice beyond that)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号