如何从当前结构中检索完整的树,或者重构当前表结构以允许优化递归查询?
问题
单个组件可以具有未定义的连接数(深度)。
无法递归更新组件受影响的属性值。
例如,如果组件的价格发生变化,则更新的所有相关组件的价格。
当前结构
组成部分
primary key (id)
| id | price |
|----|------ |
| A | 1 |
| B | 1 |
| C | 1 |
| D | 2 |
| E | 2 |
unique index (component, component_of)
index (component_of)
FK (component) References component (id)
FK (component_of) References component (id)
| component | component_of |
|--------------------------|
| D | B |
| D | C |
| B | A |
| E | C |
| E | A |
结果图模型:
UPDATE component
SET price = 2
WHERE id = 'A';
(*
指示递归更新的值
| id | price |
|----|------ |
| A | 2 |
| B | 2 | *
| C | 1 |
| D | 3 | *
| E | 3 | *
我想我需要将整个树关系存储在component\u闭包表中,以便能够检索所有组件关系的component\u,并使用深度列来确定组件的顺序。尽管在不需要完整树的情况下(例如对于的直接组件u),这似乎是浪费。
例如:
| component | component_of | depth |
|-----------|--------------|-------|
| D | A | 1 |
| D | B | 2 |
| D | C | 1 |