If you have some new MariaDB or MYSQL version and slow GROUP BY issue, you can try explain your slow query and find many 'LATERAL DERIVED' select type, so you can try change optimizer:
MariaDB [db]> set optimizer_switch='split_materialized=off';
Query OK, 0 rows affected (0.000 sec)
To set permanently vi /etc/my.cnf.d/server.cnf
[mysqld]
optimizer_switch=split_materialized=off