SELECT with query variables not using INDEXes
===

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 on the reason 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, 
      (SELECT @last_id := 5, @depth := -1) vars
    WHERE id = 5 OR parent_id = @last_id OR parent_id = 5

>[See a working example (with Schema and small data) at SQLfiddle](http://sqlfiddle.com/#!2/d7a62/2)


----------

**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 just want to understand (in detail) why MySQL is not using an `INDEX` in this case.