I'm new to MySQL. The GetFamilyTree(id) function that Rolando suggested in the Find highest level of a hierarchical field: with vs without CTEs question was great but when the id is greater than 999 it hangs.

Is there a solution that works with larger ids?

2 Answers 2


There are several things you can do


Expand all VARCHAR(1024) to VARCHAR(10240) in all Stored Procedures. This will accommodate longer Family Tree Listings. This is vital because queue_children can grow longer and longer if you have a lopsided tree structure. It also possible that when queue_children was 1024 in length and did not delimit properly, it probably went into some infinite loop trying to terminate properly and never reached the condition when the LENGTH(queue_children) would be 0.


GROUP_CONCAT()'s maximum length is limited by group_concat_max_len (default is 1024). Try expanding group_concat_max_len to something ridiculously large (5000000000).


Increase your thread_stack, perhaps 512K or 1M. This is needed for Stored Procedures with either recursive operations or, in this case, longer local variables to make room for.


When I changed the part "FORMAT(queue,0)" into simply "queue", GetFamilyTree worked for IDs over 999. This is because FORMAT(9999,0) = "9,999" in my system at least.

I also added a unique index under id and a non-unique index under parent_id and the code works well on a dataset with 230k rows. I also changed VARCHAR(1024) to TEXT.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.