In a general one-to-many (parent-to-child) relationship, is there a significant efficiency difference between (a) putting parent_id in the child table and (b) using a pivot table of only parent_id, child_id?
NOTE: Assume Oracle if necessary, otherwise answer using a general RDBMS.
If by
PIVOTtable you mean amany-to-manylink table, then no, it will only hamper performance.You should keep
parent_idin the child table.The
many-to-manylink table takes an extraJOINand therefore is less efficient.Compare the following queries:
and this one:
The latter one is one
JOINshorter and more efficient.The only possible exception to that rule is that you run these queries often:
, i. e. you know the
id‘s of the child rows beforehand.This may be useful if you use natural keys for your
child_table.In this case yes, the
child_to_parentlink table will be more efficient, since you can just replace it with the following query:and
child_to_parentwill be always less or equal in size tochild_table, and hence more efficient.However, in
Oracleyou can achieve just the same result creating a composite index onchild_table (id, parent_id).Since
Oracledoes not indexNULL‘s, this index will be just like yourchild_to_parenttable, but without the table itself and implied maintenance overhead.In other systems (which index
NULL‘s), the index may be less efficient than a dedicated table, especially if you have lots ofNULLparents.