关于innodb_thread_concurrency参数 并发控制

时间:2023-03-09 19:50:56
关于innodb_thread_concurrency参数 并发控制

http://dev.mysql.com/doc/refman/5.5/en/innodb-parameters.html#sysvar_innodb_thread_concurrency

Command-Line Format	--innodb_thread_concurrency=#
Option-File Format innodb_thread_concurrency
Option Sets Variable Yes, innodb_thread_concurrency
Variable Name innodb_thread_concurrency
Variable Scope Global
Dynamic Variable Yes
Permitted Values
Type numeric
Default 0
Range 0 .. 1000

InnoDB tries to keep the number of operating system threads concurrently inside InnoDB less than or equal to the limit given by this variable. Once the number of threads reaches this limit, additional threads are placed into a wait state within a FIFO queue for execution. Threads waiting for locks are not counted in the number of concurrently executing threads.

The correct value for this variable is dependent on environment and workload. Try a range of different values to determine what value works for your applications. A recommended value is 2 times the number of CPUs plus the number of disks.

The range of this variable is 0 to 1000. A value of 0 (the default) is interpreted as infinite concurrency (no concurrency checking). Disabling thread concurrency checking enables InnoDB to create as many threads as it needs.

参数的含义是: InnoDB内部的并发线程数.
可以动态修改
具体解析: InnoDB 试图保持InnoDB内部的并发操作系统的线程数少于innodb_thread_concurrency设置的值
如果innodb并发线程数快要到达innodb_thread_concurrency=x,其他的innodb线程会被设置为等待状态,队列的算法是FIFO
处于等待拿锁状态的线程数,不会被计算入正在执行的并发线程数

innodb_thread_concurrency=x,x该设怎样的值,视服务器配置和服务器的负载情况。
默认推荐的值是 (cpu的数量+磁盘数量)x2 (我的理解,对于raid10的磁盘阵列,应该是磁盘总数/2)

参数取值范围 0-1000 当为默认值的时候,不是说0个并发线程。 而是被解释为无限并发(没有并发检查)
当innodb_thread_concurrency=0的时候,可以理解为 禁用线程并发检查,使InnoDB按照请求的需求, 创造尽可能多的线程.

http://www.dbathink.com/2012/10/trouble-shooting-the-high-sys-cpu-in-mysql-server/

并发控制

  并发控制点:

并发控制的目的是最大化提高系统的资源利用率,并减少管理和调度开销。在MySQL实例中,主要处理sql请求,所以期望系统资源最大化提供给sql的执行过程。

  sql的执行牵涉到server层和引擎层:

1. server层:比如cost计算,生成sql执行计划的过程
2. Innodb层:比如根据执行计划,查找和更新数据page的过程

  所以在MySQL实例中,有两个最佳的并发控制点:

1. server层:sql开始执行时。 MySQL在5.6后,在server层引入了thread pool进行并发控制
2. Innodb层:记录查找和记录更新时。 Innodb存储引擎,使用innodb_thread_concurrency参数进行并发控制

  并发控制大小:

设置过大:造成系统调度消耗过大
设置过小:不能完全的使用系统资源,造成资源浪费

  经验值:# Try number of CPU's*2 for thread_concurrency

  但还需要配合具体的平台和业务系统进行测试,才能找到最佳值。

Innodb并发控制

  Innodb使用参数innodb_thread_concurrency控制并发线程的个数,源码中使用一对函数:

innodb_srv_conc_enter_innodb
innodb_srv_conc_exit_innodb

  Innodb实现语句级的并发控制,在语句执行结束,stmt commit的时候,强制释放资源。

权衡和优化

1. 一方面进行并发控制,提高资源利用率,
2. 另一方还需要控制调度公平,防饿死等。

  Innodb引入了n_tickets_to_enter_innodb参数,sql进入innodb执行时进行初始化,默认值500。

  在执行过程中,依次进行递减,递减到0时,强制退出并发线程,重新抢占。

  好处:

1. 一方面单条sql可能写入或者更新多条记录,节省每次enter innodb的线程抢占代价。
2. 另一方面防止单条sql过多的长时间占用并发线程,导致其它线程饿死的情况