I’m working with a hierarchical data structure in MySQL where I need to maintain calculated totals. Here’s my table setup:
CREATE TABLE nodes (
id INT,
sum_value INT,
PRIMARY KEY (id)
);
CREATE TABLE hierarchy (
parent_id INT,
child_id INT,
FOREIGN KEY (parent_id, child_id) REFERENCES nodes (id, id)
);
The setup works like this: leaf nodes have their sum_value set manually, but parent nodes should always contain the sum of all their children’s values. I’ve been trying this query to recalculate everything:
UPDATE nodes SET sum_value = (
SELECT SUM(child.sum_value) FROM
hierarchy JOIN nodes AS child
ON hierarchy.child_id = child.id
WHERE hierarchy.parent_id = nodes.id
)
WHERE EXISTS (
SELECT * FROM hierarchy WHERE parent_id = nodes.id
);
The main challenge is keeping these totals accurate when data changes. I need to handle updates to leaf values and moving nodes around while keeping the tree structure intact.
What’s the most efficient way to maintain these recursive totals without recalculating everything each time?
I’ve considered a few options but each has problems:
- Recalculating everything after each change (too slow)
- Using triggers to update parent nodes automatically (MySQL limitations make this tricky)
- Creating a queue system to process updates in batches (complex to implement)
I’m looking for a solution that could work for similar aggregation scenarios too. Any ideas on the best approach here?