I have a huge employee data in mysql with an attribute as parent id which stores the supervisor of each employee and is defined in a hierarchy. Each user is working under other employee and is handling team of 4-5 members. I frequently need the supervisor or subordinates tree for which i am using a recursive function to fetch an employ with its team. Please suggest me a method so that I don’t have to call recursive function each time I need employee data. Is using “Views or stored procedure” a good idea?
Thanks.
When you use stored procedures, you will still need recursion. You only move the recursion from the PHP source code to the database.
You can use nested sets to store hierarchical data. This gets rid of recursions at the price of higher costs for insertion, deletion and relocation. Basically, you create two additional fields
leftandrightwhereleft < rightande1is subordinate ofe2iffe1.left > e2.left && e1.right < e2.right.This makes SELECT queries hard to read, but efficient. Do this when all else fails.