更新时间:2023-10-09 GMT+08:00

连接线程池

功能介绍

当数据库并发连接过多时,存在大量资源竞争,MySQL服务器的性能会发生明显的下降。华为云RDS for MySQL提供了连接线程池,能够以少量活跃线程来服务大量数据库连接,将连接和执行解耦,在高并发场景下提高数据库性能。

功能特点

华为云RDS for MySQL连接线程池具有以下特点:

  • 避免性能瓶颈,可以处理大量数据库连接,减少资源的争抢和上下文切换。
  • 限制并发事务的数量,当数据库负载较高时,优先保障正在执行的事务。
  • 避免由于连接得不到及时处理而出现线程异常问题。
  • 当事务在等待IO和锁时,释放CPU资源以服务其他连接。

线程池相关操作

  • 查询线程池参数

    通过show variables命令查询线程池参数。

    show variables like 'threadpool%';
    +-----------------------------------+------------+
    | Variable_name                     | Value      |
    +-----------------------------------+------------+
    | threadpool_enabled                | ON         | 
    | threadpool_high_prio_tickets      | 4294967295 | 
    | threadpool_idle_timeout           | 60         |
    | threadpool_long_conn_time         | 2          |
    | threadpool_max_threads            | 100000     | 
    | threadpool_oversubscribe          | 3          |
    | threadpool_prio_kickup_timer      | 1000       | 
    | threadpool_rec_launch_time        | ON         | 
    | threadpool_size                   | 128        | 
    | threadpool_slow_conn_log          | ON         |
    | threadpool_slow_conn_log_interval | 30         |
    | threadpool_slow_launch_time       | 2          | 
    | threadpool_stall_limit            | 500        | 
    +-----------------------------------+------------+
    表1 线程池参数说明

    参数名

    参数描述

    threadpool_enabled

    开启或关闭线程池。

    threadpool_high_prio_tickets

    高优先级线程持有的tickets数量。

    threadpool_idle_timeout

    线程销毁前等待的空闲时间(单位:秒)。

    threadpool_long_conn_time

    登录时间超过此值,则在日志中打印此次登录信息。

    threadpool_max_threads

    线程池共能创建的线程上限。

    threadpool_oversubscribe

    每个线程组中最多能创建的额外线程数。

    threadpool_prio_kickup_timer

    低优先级队列中最长停留时间(单位:毫秒)。

    threadpool_rec_launch_time

    记录线程启动时间。

    threadpool_size

    线程组数量。

    threadpool_slow_conn_log

    是否记录慢登录到error log。

    threadpool_slow_conn_log_interval

    记录频率,记录一次慢登录后,在此时间间隔内不再记录。

    threadpool_slow_launch_time

    如登录、查询时间大于此值,则status中 threadpool_slow_launch_request增加1。

    threadpool_stall_limit

    线程组忙碌检查间隔。

    表2 线程池可修改参数说明

    参数名

    是否是动态参数

    数据类型

    取值范围

    说明

    threadpool_enabled

    boolean

    [ON,OFF]

    • ON:开启线程池。
    • OFF:关闭线程池。

    threadpool_oversubscribe

    integer

    [1,50]

    每个线程组中最多能创建的额外线程数。

    threadpool_size

    integer

    [1,512]

    线程组数量。

  • 查询线程池状态

    通过show status命令查询线程池状态。

    show status like 'threadpool%';
    +------------------------------------+------------+
    | Variable_name                      | Value      |
    +------------------------------------+------------+
    | Threadpool_active_connections      | 65         | 
    | Threadpool_active_threads          | 5          | 
    | Threadpool_avg_launch_time         | 0          | 
    | Threadpool_dump_threads            | 0          | 
    | Threadpool_idle_threads            | 63         |
    | Threadpool_running                 | ON         | 
    | Threadpool_slow_launch_request     | 0          | 
    | Threadpool_threads                 | 237        | 
    | Threadpool_threads_high_water_mark | 1075       | 
    | Threadpool_waiting_threads         | 57         | 
    | Threadpool_worst_launch_time       | 692548     | 
    +------------------------------------+------------+
    表3 线程池状态说明

    状态

    说明

    Threadpool_active_connections

    线程池中活跃连接数。

    Threadpool_active_threads

    线程池中活跃线程数。

    Threadpool_avg_launch_time

    平均等待时间(单位:毫秒)。

    Threadpool_dump_threads

    dump线程数量。

    Threadpool_idle_thread

    线程池空闲线程数。

    Threadpool_running

    线程池是否在运行。

    Threadpool_slow_launch_request

    超过slow_launch_request的次数。

    Threadpool_threads

    线程池总连接数。

    Threadpool_threads_high_water_mark

    历史高位线程数量。

    Threadpool_waiting_threads

    waiting线程池状态。

    Threadpool_worst_launch_time

    最坏启动时间(单位:毫秒)。

性能测试

表4 不同线程的性能测试

模型

线程

线程池开关是否开启

QPS(每秒查询数)

latency(时延,单位是毫秒)

oltp_update_non_index

32

5932.47

7.84

oltp_update_non_index

64

10074.11

9.39

oltp_update_non_index

128

18079.61

10.65

oltp_update_non_index

256

27439.38

14.46

oltp_update_non_index

512

33007.96

28.16

oltp_update_non_index

1024

30282.13

51.94

oltp_update_non_index

2048

29836.86

95.81