I am working with a table in MySQL that defines a tree hierarchy using the "adjacency list" method, which should be fine for my purposes.
I need to compute the maximum of some value over all children using the (fast) query
SELECT MAX(N.someValue) AS rate
FROM `nodes` N
WHERE N.parent = <some node id>;
Sometimes I am not as lucky and have to work with descendants of the children (its defined, and always references some leaf node in the branch of that tree).
SELECT MAX(N.someValue) AS rate
FROM `nodes` N
WHERE N.id IN (SELECT N2.descendant FROM `nodes` N2 WHERE N2.parent = <some node id>);
This second query is quite slow. The number of children for a given parent is quite low, rarely more than 10, never more than 20. It does not appear to be a correlated subquery by my eye, though EXPLAIN says the subquery is dependent. I am testing in MySQL 5.1. nodes.id
is the primary key and there is a BTREE index on nodes.parent
. Is there a way I can improve the speed of this query?