## Module 5: Control The Performance Of Mixed Workloads With Resource Control

  ![40%](https://lab-static.pingcap.com/quick-demo/demo-006-4-en.png)

### Step 1. Reset the default resource group settings for users

+ On [Terminal 1](ssh://monitor1-ssh#1), review and run the script to reset `userA` and `userB` to the default resource group.

  ```bash
  cat lab4-rc-default.sh
  ```

  ```output
  #!/bin/bash

  source ./hosts-env.sh

  # For cluster
  export MYSQL_PS1="tidb:db1> "
  mysql -h ${HOST_DB1_PRIVATE_IP} -P 4000 -u root --verbose << 'EOFX'
  ALTER USER userA RESOURCE GROUP default;
  ALTER USER userB RESOURCE GROUP default;
  DROP RESOURCE GROUP IF EXISTS rg1;
  DROP RESOURCE GROUP IF EXISTS rg2;
  SELECT * FROM information_schema.resource_groups;
  SELECT Host,user, User_attributes FROM mysql.user WHERE user IN ('userA','userB');
  EOFX
  ```

  ```bash
  ./lab4-rc-default.sh
  ```

  ```output
  ...
  --------------
  SELECT * FROM information_schema.resource_groups
  --------------

  NAME	RU_PER_SEC	PRIORITY	BURSTABLE	QUERY_LIMIT	BACKGROUND
  default	UNLIMITED	MEDIUM	YES	NULL	NULL
  --------------
  SELECT Host,user, User_attributes FROM mysql.user WHERE user IN ('userA','userB')
  --------------

  Host	user	User_attributes
  %	userA	{"resource_group": "default"}
  %	userB	{"resource_group": "default"}
  ```

### Step 2. Start the mixed workloads under the default resource group setting

+ On [Terminal 1](ssh://monitor1-ssh#1), run the script to start a sample OLTP workload by `userA`. Keep the script running and proceed to the next step.

  ```bash
  ./lab4-workload-userA.sh
  ```

+ On [Terminal 2](ssh://monitor1-ssh#2), run the script to start a sample batch workload by `userB` (no output). Keep the script running and proceed to the next step.

  ```bash
  ./lab4-workload-userB.sh
  ```

### Step 3. Observe the result under the default resource group setting

+ On [Terminal 1](ssh://monitor1-ssh#1), observe the QPS of userA's OLTP workload. Note that the performance is negatively impacted by userB's batch workload.

  ![60%](https://lab-static.pingcap.com/quick-demo/demo-006-lab-4-1.jpeg)

+ Press <kbd>Ctrl</kbd>+<kbd>C</kbd> to stop both workloads before proceeding to the next step.

### Step 4. Use Resource Group Settings to prioritize the selected workload

+ On [Terminal 1](ssh://monitor1-ssh#1), run the script to set up resource groups for `userA` as `(RU_PER_SEC=50000 PRIORITY=HIGH BURSTABLE)` and `userB` as `(RU_PER_SEC=500 PRIORITY=MEDIUM)`.

  ```bash
  cat lab4-rc-setup.sh
  ```

  ```output
  #!/bin/bash

  source ./hosts-env.sh

  # For cluster
  export MYSQL_PS1="tidb:db1> "
  mysql -h ${HOST_DB1_PRIVATE_IP} -P 4000 -u root --verbose << 'EOFX'
  ALTER USER userA RESOURCE GROUP default;
  ALTER USER userB RESOURCE GROUP default;
  DROP RESOURCE GROUP IF EXISTS rg1;
  DROP RESOURCE GROUP IF EXISTS rg2;
  CREATE RESOURCE GROUP rg1 RU_PER_SEC=50000 PRIORITY=HIGH BURSTABLE;
  CREATE RESOURCE GROUP rg2 RU_PER_SEC=500 PRIORITY=MEDIUM;
  ALTER USER userA RESOURCE GROUP rg1;
  ALTER USER userB RESOURCE GROUP rg2;
  SELECT * FROM information_schema.resource_groups;
  SELECT Host,user, User_attributes FROM mysql.user WHERE user IN ('userA','userB');
  EOFX
  ```

  ```bash
  ./lab4-rc-setup.sh
  ```

  ```output
  ...
  --------------
  SELECT * FROM information_schema.resource_groups
  --------------

  NAME	RU_PER_SEC	PRIORITY	BURSTABLE	QUERY_LIMIT	BACKGROUND
  default	UNLIMITED	MEDIUM	YES	NULL	NULL
  rg1	50000	HIGH	YES	NULL	NULL
  rg2	500	MEDIUM	NO	NULL	NULL
  --------------
  SELECT Host,user, User_attributes FROM mysql.user WHERE user IN ('userA','userB')
  --------------

  Host	user	User_attributes
  %	userA	{"resource_group": "rg1"}
  %	userB	{"resource_group": "rg2"}
  ```

### Step 5. Start the mixed workloads again

+ On [Terminal 1](ssh://monitor1-ssh#1), run the script to start a sample OLTP workload again by `userA`, keep the script running.

  ```bash
  ./lab4-workload-userA.sh
  ```

+ On [Terminal 2](ssh://monitor1-ssh#2), run the script to start a sample batch workload again by `userB` (no output), keep the script running.

  ```bash
  ./lab4-workload-userB.sh
  ```

### Step 6. Observe the result

+ On [Terminal 1](ssh://monitor1-ssh#1), observe the QPS of userA's OLTP workload, after the QPS become stable, which may take up to 1 minute. Note that the performance is much better than the result from Step 4.

  ![60%](https://lab-static.pingcap.com/quick-demo/demo-006-M5-mixed-workloads-with-RC-v3-20250331.png)

+ Press <kbd>Ctrl</kbd>+<kbd>C</kbd> on [Terminal 1](ssh://monitor1-ssh#1) and [Terminal 2](ssh://monitor1-ssh#2) to stop both workloads.
