MySQL 8.0 is out. Is this a great piece of news? No.
But MySQL 8.0 is wonderful, and this IS a great piece of news!
It has lots of interesting features, really. Oracle advertised some of them very well with talks at conferences, blog posts, etc. However I am very pleased by a features that they did not advertised at all: resource groups.
The documentation describes them in detail, but here is a small recap.
As we all know, MySQL has system (background) threads, and user (foreground) threads. Until now, nothing could be done to change their priority. All we could do was to tune InnoDB concurrency tickets to make sure that long running queries don’t prevent other queries from using CPU time. Basically, what we do is splitting the execution of a query to improve concurrency.
Now we can also affect threads priority. By default, all threads have the same priority. We can increase the priority of system threads, or decrease the priority of user threads. In any case, no user thread can have more priority than any system thread. Furthermore, we can limit the execution of some threads to one or more virtual CPUs of our choice.
How are these things done? By creating a resource group and setting
THREAD_PRIORITY is a number (-20..0 for system threads, 0..19 for user threads; lower numbers have higher priority).
VCPU is a virtual CPU number (see which ones are available with
lscpu), or a range, or a list of numbers/ranges. Then, we can assign running threads to one of the groups we created.
For example, to create a group for system threads:
CREATE RESOURCE GROUP sql_thread
TYPE = SYSTEM
VCPU = 0,1
THREAD_PRIORITY = -10;
To assign an existing thread to that group, check its id with
SHOW PROCESSLIST, and then run something like:
SET RESOURCE GROUP sql_thread FOR 10;
Some possible ideas:
- Increase priority of replication threads to reduce slave lag;
- Increase priority of the even scheduler;
- Decrease priority of long-running analytical queries.
I plan to spend some time to experiment what happens if we change the priority of specific system threads on a busy server. I expect this feature to be helpful during peaks of CPU usage.
Unfortunately, resource groups can only be assigned to running threads. There is no way to tell MySQL that connections from a certain user should use a certain group. However, we can achieve the same result by making a change in the applications. After connecting, the application can run:
SET RESOURCE GROUP my_group;