For detailed explanations, see my blog post:
If your users can be found more than once in the needs column of a table, this is a difficult NP task.
If not, specify this query in your view:
SELECT COUNT(*) FROM v_needs CONNECT BY NOCYCLE need = PRIOR have WHERE CONNECT_BY_ISCYCLE = 1
CONNECT BY NOCYCLE allows loops inside hierarchical queries ( NOCYCLE just stops the branch when it finds loops, no NOCYCLE returns an error).
CONNECT_BY_ISCYCLE returns true whenever it finds a loop (it returns 1 for a record that would give the root of the current branch in the next iteration).
Please note that this query will give you all users in cycles without grouping them.
To group users, run this query:
SELECT n.*, CONNECT_BY_ROOT(have), level FROM v_needs n START WITH have IN ( SELECT MIN(have) FROM ( SELECT have, CONNECT_BY_ROOT(have) AS root FROM t_needs START WITH have IN ( SELECT have FROM t_needs n WHERE CONNECT_BY_ISCYCLE = 1 CONNECT BY NOCYCLE needs = PRIOR have ) CONNECT BY NOCYCLE have = PRIOR needs ) GROUP BY root ) CONNECT BY NOCYCLE have = PRIOR needs
Update:
From your post, I see that you have a limit on the maximum loop length.
This greatly facilitates the solution to this problem.
Just add a loop constraint condition to each of the queries:
SELECT n.*, CONNECT_BY_ROOT(have), level FROM v_needs n START WITH have IN ( SELECT MIN(have) FROM ( SELECT have, CONNECT_BY_ROOT(have) AS root FROM t_needs START WITH have IN ( SELECT have FROM t_needs n WHERE CONNECT_BY_ISCYCLE = 1 CONNECT BY NOCYCLE needs = PRIOR have AND level <= 5 ) CONNECT BY NOCYCLE have = PRIOR needs AND level <= 5 ) GROUP BY root ) CONNECT BY NOCYCLE have = PRIOR needs AND level <= 5
This will stop the hierarchy tree from moving at 5th level.
If you have 1,000,000 users in your database, and 5 corresponds to each user on average, the query will have to examine the rows 1,000,000 * 5^5 = 3,125,000,000 , which is the observed number of rows.
The query will be greatly improved if you MATERIALIZE your presentation and create indexes on "need" and "have".
In this case, the request will be completed in a matter of minutes.