## 模块 4: 使用流量可视化页面 (Key Visualizer)

+ 在本练习中，你将使用 sysbench 工具模拟并发数据插入，并采用 `AUTO_RANDOM` 选项尝试拆分热点。

### 步骤 1. 配置 [TiDB PD Dashboard](http://${LAB:HOST_PD1_PUBLIC_IP}:2379/dashboard) `key visualizer`

+ 打开 [TiDB PD Dashboard](http://${LAB:HOST_PD1_PUBLIC_IP}:2379/dashboard)，默认用户名是 `root`，无密码。
+ 在 [TiDB PD Dashboard](http://${LAB:HOST_PD1_PUBLIC_IP}:2379/dashboard) 的 `key visualizer` 中，将刷新间隔设置为 `30s`。

### 步骤 2. 阅读脚本 `oltp_common_1.lua`

+ 请注意  `CREATE TABLE` 语句。思考使用的表创建方法可能会出现什么问题。

   ```bash
   cat oltp_common_1.lua 
   ```

   ```output
   cat oltp_common_1.lua 
   ...
   query = string.format([[
   CREATE TABLE sbtest%d(
     id INT(11) NOT NULL AUTO_INCREMENT,
     k INT(11) NOT NULL DEFAULT '0',
     c CHAR(120) NOT NULL DEFAULT '',
     pad CHAR(60) DEFAULT '' NOT NULL,
     PRIMARY KEY (id)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin]],
         table_num)

   con:query(query)
   ...
   ```

+ 创建的表为主键创建了聚簇索引。它在主键列 `id` 上使用了 `AUTO_INCREMENT`。这可能会在并发插入时导致热点问题。

### 步骤 3. 查看 sysbench 工具的配置文件

   ```bash
   cat config_lab01
   ```

   ```output
   mysql-host=10.90.1.178 
   mysql-port=4000 
   mysql-user=root 
   mysql-db=sbtest 
   mysql-password=
   time=600 
   threads=32 
   report-interval=10 
   db-driver=mysql
   ```

+ 以下是更详细的解释：
  + time: 测试的运行时间。
  + thread: 并发会话数。
  + report-interval: 打印报告的间隔（单位：秒）。
  + db-driver: MySQL 兼容语法。

### 步骤 4. 创建一个新的数据库用于 sysbench 测试

   ```bash
   mysql -h ${HOST_DB1_PRIVATE_IP} --port 4000 -u root
   ```

   ```sql
   CREATE DATABASE sbtest;
   EXIT;
   ```

### 步骤 5. 运行 sysbench 模拟并发数据插入场景

+ 此步骤可能需要长达 5 分钟，在此过程中，我们可以通过 [TiDB PD Dashboard](http://${LAB:HOST_PD1_PUBLIC_IP}:2379/dashboard) 中的流量可视化图表监控是否生成热点。

   ```bash
   sysbench './oltp_common_1.lua' --config-file=config_lab01 --tables=1 --table-size=5000000 prepare
   ```

   ```output
    sysbench './oltp_common_1.lua' --config-file=config_lab01 --tables=1 --table-size=5000000 prepare
   sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

   Initializing worker threads...

   Creating table 'sbtest1'...
   Inserting 20000000 records into 'sbtest1'
   ```

+ table-size : 将插入表中的行数。
+ sysbench 测试完成后，在 [TiDB PD Dashboard](http://${LAB:HOST_PD1_PUBLIC_IP}:2379/dashboard) 的流量可视化图表中观察 1 小时内的写入流量。以下图表是插入 20,000,000 条数据后生成的。你的图表可能不同。思考为什么会出现这种情况以及是否可以优化。

   ![30%](https://lab-static.pingcap.com/quick-demo/TableAndIdnexDesignSysBench1_aa_001_20231121.png)
  + 因为该表是主键的聚簇索引，并使用 `AUTO_INCREMENT` 定义主键，在持续插入数据时，我们发现同时写入压力集中在某个区域，这意味着存在热点问题。

### 步骤 6. 清理 `oltp_common_1.lua` 的数据

   ```bash
   sysbench './oltp_common_1.lua' --config-file=config_lab01 cleanup
   ```

   ```output
    sysbench './oltp_common_1.lua' --config-file=config_lab01 cleanup
   sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

   Dropping table 'sbtest1'...
   ```

### 步骤 7. 阅读脚本 `oltp_common_2.lua`

+ 请注意 `CREATE TABLE` 语句。

   ```bash
   cat oltp_common_2.lua 
   ```

   ```output
   ...
   query = string.format([[
   CREATE TABLE sbtest%d(
   id bigINT(11) NOT NULL AUTO_RANDOM(5),
   k INT(11) NOT NULL DEFAULT '0',
   c CHAR(120) NOT NULL DEFAULT '',
   pad CHAR(60) DEFAULT '' NOT NULL,
   PRIMARY KEY (id)
   ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin]],
         table_num)

   con:query(query)
   ...
   ```

### 步骤 8. 运行 sysbench 模拟并发数据插入场景

+ 同样，此步骤可能需要 5 分钟。在此过程中，我们可以使用 [TiDB PD Dashboard](http://${LAB:HOST_PD1_PUBLIC_IP}:2379/dashboard) 中的流量可视化图表监控是否生成热点。

   ```bash
   sysbench './oltp_common_2.lua' --config-file=config_lab01 --tables=1 --table-size=5000000 prepare
   ```

   ```output
   sysbench './oltp_common_2.lua' --config-file=config_lab01 --tables=1 --table-size=5000000 prepare
   sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

   Initializing worker threads...

   Creating table 'sbtest1'...
   Inserting 5000000 records into 'sbtest1'
   ```

+ sysbench 测试完成后，在 [TiDB PD Dashboard](http://${LAB:HOST_PD1_PUBLIC_IP}:2379/dashboard) 的流量可视化图表中观察 1 小时内的流入流量。以下图表是插入 20,000,000 条数据后生成的。你的图表可能不同。思考原因并与之前的测试进行比较，判断热点问题是否已解决。

   ![30%](https://lab-static.pingcap.com/quick-demo/TableAndIdnexDesignSysBench2_aa_001_20231121.png)

### 步骤 9. 清理 `oltp_common_2.lua` 的数据

   ```bash
   sysbench './oltp_common_2.lua' --config-file=config_lab01 cleanup
   ```

   ```output
    sysbench './oltp_common_2.lua' --config-file=config_lab01 cleanup
   sysbench 1.0.17 (using bundled LuaJIT 2.1.0-beta2)

   Dropping table 'sbtest1'...
   ```
