Look at this 1st scenario, you got a table that has 2 columns- Parent (P) & Child (C).
P-C 1-3 2-8 3-6 6-4 8-7
When users search for all descendants of "1" then it will show:
P-C 1-3 3-6 6-4
& When users search for all descendants of "2" then it will show:
P-C 2-8 8-7
This is the Mysql query to get the Data
select distinct col1
from (select col1,
@pv:=(case when find_in_set(col2, @pv) then @pv else concat(@pv, ',', col2)
end) as 'col2'
from table1 join
(select @pv:='1') tmp
on find_in_set(col1, @pv) > 0
) t
Ok, you know that Database indexing is to index the column so that the DB can look up DB faster than without indexing.
However, in the 1st scenario mentioned above, "Do you think DB indexing play any important role in Parent Child table?"
Ok, if the users search for all descendants of "2", then the DB first found "2-8", then it has to jump over 2 records before it found the next child "8-7".
This is the simple example, but what if there're thousands of records located far away from each other (or position of data is very fragmented), then "How DB (assuming that the Parent-Child column got indexed) can look up data quickly in the 1st scenario?"
But if we make all the descendants sit next to each other like in this 2nd scenario:
P-C 1-3 3-6 6-4 2-8 8-7
then "Will DB (even we don't index Parent Child column) look up data faster in the 2nd scenario than the 1st one?"
Note: If you reverse the order of the descendants like this:
P-C 6-4 3-6 1-3 2-8 8-7
& if you search for "1" then it will show only "3", it won't show "3-6" & "6-4" since "3-6" & "6-4" are not in the continuous order. It means that the MYSQL, when running the above query, will search the record from the top to down. So it means that Mysql won't search for the next descendants from the beginning, -> Do you think so?
Note: Pls also read this link @ Symbol - a solution for Recursive SELECT query in Mysql?