Tune mariadb for large tables

I have a server with 128gb ram and nvme disks where the php code runs, then I have another server with 64gb ram also with nvme where the database is running.
Both servers run HestiaCP
The project has grown over time and we are at a point where in a few days the system has to perform a task that involves performing a search and arithmetic calculation based on about 100,000 records.
Then the system has to perform about 1000 new registrations.
On days with more records to perform this task, the system takes about 2m 30s, but on days of lower traffic (+/- 50,000 records) it takes less than 20 seconds…
I know that this subject is not directly related to HestiaCP, but I also know that there are good experts on these subjects here who like to help.
I desperately need to reduce the time needed to perform this task because if the amount of records continues to grow, sooner or later I’ll have a problem that I won’t be able to solve…
Thank you all for your time and patience