Perfomance issues

We have about 800k tickets in our RT 4.4.4.

When ticket is being processed support engineer fills customfield Client with client group name and one of scrips takes this group name and links this client group to the custom role Client.

Also we have a group Datacenter that has department groups as members.
These department groups named like support, engineer, manager, etc.
And staff members added to this department groups.

At now when we add new staff member to one of these department groups user may be linked to the group for around 15 minutes. Or operation may fail at all.

Because most of the tickets are internal requests, they have group Datacenter set as Client custom role.
About two months ago the count of Datacenter group tickets was about 350k.
If we open some staff user summary page like there are shown Active Tickets and Inactive Tickets sections.
All tickets linked to the Datacenter group (350k tickets) are shown at these sections.

So we thought if we remove custom role Client value from all our internal tickets maybe we can speed up the process of adding user to the department group.

If we run just one operation of unlinking ticket from the Datacenter group it may resolve in about 4 to 6 minutes. And cpu is being used very much. So it’s obvious there are some costly operations.

We processed about 85k of tickets in about 1,5 months running custom role value removing script in 10 threads. That was very cpu extensive process and now we can add user to group in 14 minutes instead of 15.

We thought there will be more benefit from unlinking tickets but now we really don’t understand will it help if we’ll continue to burn our host for about 4 months to unlink all other tickets.

How does it work and how can we diagnose what’s the problem with these operations?