I was playing around (out of interest) with retrieving a tree of nodes in a simple adjacency list with a recursive query using local variables.
The solution i have so far is fun but i wonder (and this is my only question) why MySQL refuses to use any INDEX
to optimize this query. Shouldn't MySQL be able to lookup the nearest child(s) by using an INDEX
?
I'm curious why MySQL doesn't. Even when i use FORCE INDEX
the execution plan doesn't change.
This is the query so far, with 5
being the ID of the parent node:
SELECT
@last_id := id AS id,
parent_id,
name,
@depth := IF(parent_id = 5, 1, @depth + 1) AS depth
FROM
tree FORCE INDEX (index_parent_id, PRIMARY, index_both),
(SELECT @last_id := 5, @depth := -1) vars
WHERE id = 5 OR parent_id = @last_id OR parent_id = 5
Note that the reason can't be the small dataset, because the behaviour doesn't change when i specify FORCE INDEX (id)
or FORCE INDEX (parent_id)
or FORCE INDEX (id, parent_id)
...
The docs say:
You can also use FORCE INDEX, which acts like USE INDEX (index_list) but with the addition that a table scan is assumed to be very expensive. In other words, a table scan is used only if there is no way to use one of the given indexes to find rows in the table.
There must be something that renders the query unable to use the INDEX, but i don't understand what it is.
Disclaimer: I know there are different ways to store and retrieve hierarchical data in SQL. I know about the nested sets model. I'm not looking for an alternative implementation. I'm not looking for nested sets.
I also know the query in itself is nuts and produces wrong results.
I just want to understand (in detail) why MySQL is not using an INDEX
in this case.