Low performance with large number of groups

I am creating thousands of groups to manage different organisations, each of them with different set of users assigned.

By just creating the following groups: /org1 /org2 /org3 … /org40000, the performance gets affected a lot. Mainly when calling HTTP API: GET /{realm}/groups

Selecting the groups with a SELECT in the MySQL db seems quick and straighforward, I think the problem is creating the structure parent/child that is returned in the JSON.

Currently the API call for 40k groups can take 5 minutes to process.

Any clue/solution?

bw
C