I am trying to resolve a problem which i am currently facing. I am trying to find out which is the fastest way to retrieve information from a database.
So a managers name is passed in, what i need to do is return all the users who's manager is passed in, but if any of these users happen to be a manager, i have to return all the user names (List<String>
) they manage also... this will repeat until i return everyone
here is a small example of what i need to return
manager --> manager --> manager --> manager --> employee
--> manager --> employee
--> manager --> manager --> manager --> employee
--> employee
so in the example above the code would be returning 12 names
i know i could do this a number of different ways but i do not know which would be the best way (recursive for loop, for loop recursively calling method, SQL statement, HQL statement ... etc.)
As this list can be any size depending on the manager passed in, i need to find which would be the quickest way to retrieve this as i have coded this to use recursive for loops and it takes 1 minute 20 seconds which is WAY too long
Any ideas?
What you need is to perform a performance analysis, in order to know if the latency comes from the database, the application server, or the network (latency due to many loops). According to your figures, I think you are doing too many queries, but this is an hypothesis you should verify.
In my company, which is a big company, there is no more than 15 levels between the Big Boss and any employee. Therefore, you shouldn't have to do more than 15 request.
I suspect that you do one loop for each people, in order to know it's employee. Get where manager = name What you could do is doing one HQL request to get all the employees on a list "get where manager IN (list of manager)". It should dramatically reduce the time spent, because you would do 15 request recursively instead of 13k for the big boss.
Otherwise, if you want to use an SQL statement, you might conisder using the keyword WITH.