MYSQL aggregates taking forever for nested-set-modeled data

So I have a table of hierarchical data a la:

Widgets:
id INT NOT NULL UNIQUE,
lft INT,
rgt INT,
depth INT

Open in new window


The nested set model (lft,rgt fields) allows me to discern children and parents and depths and all kinds of greatness that I couldn't get from adjacency pairs (ie parent_id field).

However, I now have related data in another table like this:

WidgetActuals:
widget_id INT,
cost DECIMAL,
period_id INT

Open in new window


Where the widget id is the widget's id and periods are like march 2013 etc (another table).  My problem is I would like to roll up the costs in a given period to the parents, something like:


SELECT p.id, a.period_id, SUM(a.value) FROM Widget c INNER JOIN WidgetActuals a ON (a.widget_id = c.id) INNER JOIN Widget p ON (c.lft BETWEEN p.lft AND p.rgt) GROUP BY p.id, a.period_id;

Open in new window



would seem to get it done, but it's too slow.
In my case I have a Widgets table of about 10k entries, and the WidgetActuals contains about 70k entries, the above query takes ~5 minutes. I'd prefer to get it down to the speed of light if possible...Any help?
FLEA2007Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
As you're roll up the entire tree, this will take a while.

I would consider using an additional path column in your structure, then the grouping can be done on the path.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
MySQL Server

From novice to tech pro — start learning today.