HiveBrain v1.2.0
Get Started
← Back to all entries
snippetsqlMinor

How can I determine the maximum possible number of threads?

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
canthemaximumnumberthreadspossibledeterminehow

Problem

What factors determine the maximum possible number of threads mysqld will create?

I only care about consideration for mysql and not limitations that may be imposed by the operating system. Further, I only care about Linux, though if you want to include information specific to Windows, feel free.

As far as I can tell, threads can be created for replication, aspects of innodb, one per client connection up to max_connections, with an additional +1 to handle super connections. What else is there, or is that it?

threads_max =   max_connections  
              + 1 for super  
              + 2 for replication slave threads  
              + ? for InnoDB ?

Solution

The best way to see current threads (including all foreground and background threads) is now with performance_schema. For example:

mysql 5.7.3> select thread_id, name, type, PROCESSLIST_ID from performance_schema.threads;
+-----------+----------------------------------------+------------+----------------+
| thread_id | name                                   | type       | PROCESSLIST_ID |
+-----------+----------------------------------------+------------+----------------+
|         1 | thread/sql/main                        | BACKGROUND |           NULL |
|         2 | thread/innodb/io_log_thread            | BACKGROUND |           NULL |
|         3 | thread/innodb/io_read_thread           | BACKGROUND |           NULL |
|         4 | thread/innodb/io_read_thread           | BACKGROUND |           NULL |
|         5 | thread/innodb/io_ibuf_thread           | BACKGROUND |           NULL |
|         6 | thread/innodb/io_read_thread           | BACKGROUND |           NULL |
|         7 | thread/innodb/io_read_thread           | BACKGROUND |           NULL |
|         8 | thread/innodb/io_write_thread          | BACKGROUND |           NULL |
|         9 | thread/innodb/io_write_thread          | BACKGROUND |           NULL |
|        10 | thread/innodb/io_write_thread          | BACKGROUND |           NULL |
|        11 | thread/innodb/io_write_thread          | BACKGROUND |           NULL |
|        14 | thread/innodb/srv_lock_timeout_thread  | BACKGROUND |           NULL |
|        15 | thread/innodb/srv_error_monitor_thread | BACKGROUND |           NULL |
|        16 | thread/innodb/srv_monitor_thread       | BACKGROUND |           NULL |
|        17 | thread/innodb/srv_master_thread        | BACKGROUND |           NULL |
|        18 | thread/innodb/srv_purge_thread         | BACKGROUND |           NULL |
|        19 | thread/innodb/page_cleaner_thread      | BACKGROUND |           NULL |
|        20 | thread/sql/signal_handler              | BACKGROUND |           NULL |
|        22 | thread/sql/one_connection              | FOREGROUND |              2 |
+-----------+----------------------------------------+------------+----------------+
19 rows in set (0.00 sec)


I believe the background threads are fixed in number (the actual count will depend on some configuration settings such as innodb_read_io_threads and the number of innodb_purge_threads etc.) The FOREGROUND threads are one per connection + a potential of thread_cache_size on top of that.

Code Snippets

mysql 5.7.3> select thread_id, name, type, PROCESSLIST_ID from performance_schema.threads;
+-----------+----------------------------------------+------------+----------------+
| thread_id | name                                   | type       | PROCESSLIST_ID |
+-----------+----------------------------------------+------------+----------------+
|         1 | thread/sql/main                        | BACKGROUND |           NULL |
|         2 | thread/innodb/io_log_thread            | BACKGROUND |           NULL |
|         3 | thread/innodb/io_read_thread           | BACKGROUND |           NULL |
|         4 | thread/innodb/io_read_thread           | BACKGROUND |           NULL |
|         5 | thread/innodb/io_ibuf_thread           | BACKGROUND |           NULL |
|         6 | thread/innodb/io_read_thread           | BACKGROUND |           NULL |
|         7 | thread/innodb/io_read_thread           | BACKGROUND |           NULL |
|         8 | thread/innodb/io_write_thread          | BACKGROUND |           NULL |
|         9 | thread/innodb/io_write_thread          | BACKGROUND |           NULL |
|        10 | thread/innodb/io_write_thread          | BACKGROUND |           NULL |
|        11 | thread/innodb/io_write_thread          | BACKGROUND |           NULL |
|        14 | thread/innodb/srv_lock_timeout_thread  | BACKGROUND |           NULL |
|        15 | thread/innodb/srv_error_monitor_thread | BACKGROUND |           NULL |
|        16 | thread/innodb/srv_monitor_thread       | BACKGROUND |           NULL |
|        17 | thread/innodb/srv_master_thread        | BACKGROUND |           NULL |
|        18 | thread/innodb/srv_purge_thread         | BACKGROUND |           NULL |
|        19 | thread/innodb/page_cleaner_thread      | BACKGROUND |           NULL |
|        20 | thread/sql/signal_handler              | BACKGROUND |           NULL |
|        22 | thread/sql/one_connection              | FOREGROUND |              2 |
+-----------+----------------------------------------+------------+----------------+
19 rows in set (0.00 sec)

Context

StackExchange Database Administrators Q#56482, answer score: 7

Revisions (0)

No revisions yet.