In my case changing the parameter sort_buffer_size from 256K to 512MB did the trick.
This is the context:
I got this error:
SQLSTATE[HY000]: General error: 3 Error writing file '/rdsdbdata/tmp/MYfd=117' (OS errno 28 - No space left on device)'
It was due to this query running several times per minute:
SELECT c.code AS code_1,
c.code AS code_2
FROM clients c
INNER JOIN clients_branch ch ON (c.id = ch.client_id)
WHERE ((CONCAT(',', ch.branch_path, ',') LIKE '%,2555,%'
OR ch.id = 2555)
AND ch.client_id <> 5552)
OR c.id = 5552
ORDER BY c.name ASC;
This query would take around 25 seconds and the number of active sessions was piling up:
It was all solved when we updated this parameter sort_buffer_size from 256K (default) to 512MB:
(you can see the drop in the bars).
We were able to check that when removing the "order by" from the query, the execution of said query would take much less.