web-dev-qa-db-fra.com

Comment puis-je déterminer le nombre maximum possible de threads?

Quels facteurs déterminent le nombre maximum possible de threads que mysqld créera?

Je me soucie uniquement de la considération pour mysql et non des limitations qui peuvent être imposées par le système d'exploitation. De plus, je ne me soucie que de Linux, mais si vous souhaitez inclure des informations spécifiques à Windows, n'hésitez pas.

Pour autant que je sache, des threads peuvent être créés pour la réplication, des aspects d'innodb, un par connexion client jusqu'à max_connections, avec un +1 supplémentaire pour gérer les super connexions. Qu'y a-t-il d'autre ou est-ce que c'est?

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

Sous Linux par exemple, selon la plus petite des valeurs suivantes:

grep -F 'Max process' /proc/$(/sbin/pidof mysqld)/limits|awk '{print $3}'

et:

mysql -e'show global variables like "max_connections"'

MISE À JOUR: Comme vous l'avez correctement souligné, nous devons ajouter au moins les fils d'arrière-plan à la liste. Plus d'informations ci-dessous.

En ce qui concerne votre dernière mise à jour, pour InnoDB, nous avons innodb_read_io_threads et innodb_write_io_threads .

Je viens de vérifier et un MySQL 5.5.34 sur debian avec les valeurs par défaut que nous avons:

mysql> pager grep -i thread
PAGER set to 'grep -i thread'
mysql> show engine innodb status\G
BACKGROUND THREAD
srv_master_thread loops: 1 1_second, 1 sleeps, 0 10_second, 1 background, 1 flush
srv_master_thread log flush and writes: 2
MySQL thread id 36, OS thread handle 0xa6c32b40, query id 105 localhost root
I/O thread 0 state: waiting for i/o request (insert buffer thread)
I/O thread 1 state: waiting for i/o request (log thread)
I/O thread 2 state: waiting for i/o request (read thread)
I/O thread 3 state: waiting for i/o request (read thread)
I/O thread 4 state: waiting for i/o request (read thread)
I/O thread 5 state: waiting for i/o request (read thread)
I/O thread 6 state: waiting for i/o request (write thread)
I/O thread 7 state: waiting for i/o request (write thread)
I/O thread 8 state: waiting for i/o request (write thread)
I/O thread 9 state: waiting for i/o request (write thread)
Main thread process no. 21508, id 2758552384, state: waiting for server activity
1 row in set (0.00 sec)

mysql>

Et l'OS voit:

root@ubuntu:~# ps -Lp$(pidof mysqld) |wc -l
18

Après la connexion en tant que root, le nombre de threads est resté le même (le nombre de threads a augmenté lorsque j'en ai rendu un autre difficile):

root@ubuntu:~# mysql -proot
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 37
Server version: 5.5.34-0ubuntu0.12.04.1 (Ubuntu)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> \! ps -Lp$(pidof mysqld) |wc -l
18
mysql> show global status like '%thread%';
+------------------------------------------+-------+
| Variable_name                            | Value |
+------------------------------------------+-------+
| Delayed_insert_threads                   | 0     |
| Performance_schema_thread_classes_lost   | 0     |
| Performance_schema_thread_instances_lost | 0     |
| Slow_launch_threads                      | 0     |
| Threads_cached                           | 0     |
| Threads_connected                        | 1     |
| Threads_created                          | 1     |
| Threads_running                          | 1     |
+------------------------------------------+-------+
8 rows in set (0.01 sec)

mysql> show global variables like '%thread%';
+-----------------------------------------+---------------------------+
| Variable_name                           | Value                     |
+-----------------------------------------+---------------------------+
| innodb_purge_threads                    | 0                         |
| innodb_read_io_threads                  | 4                         |
| innodb_thread_concurrency               | 0                         |
| innodb_thread_sleep_delay               | 10000                     |
| innodb_write_io_threads                 | 4                         |
| max_delayed_threads                     | 20                        |
| max_insert_delayed_threads              | 20                        |
| myisam_repair_threads                   | 1                         |
| performance_schema_max_thread_classes   | 50                        |
| performance_schema_max_thread_instances | 1000                      |
| thread_cache_size                       | 8                         |
| thread_concurrency                      | 10                        |
| thread_handling                         | one-thread-per-connection |
| thread_stack                            | 196608                    |
+-----------------------------------------+---------------------------+
14 rows in set (0.00 sec)

Donc, comme je l'ai dit, cela pourrait être spécifique à la version, mais nous semblons avoir:

  • srv_master_thread
  • n Threads d'E/S (innodb_read_io_threads, innodb_write_io_threads, insert buffer buffer, log thread)
  • autres threads d'arrière-plan (7 dans ce cas: total 18 - 10 (E/S) - 1 (principal)).

Donc, si vous devez faire une sorte de planification de la capacité, je suggère de vérifier le nombre de threads d'arrière-plan après le démarrage sans connexion utilisateur/application et d'ajouter à cela:

  1. 1 pour les utilisateurs avec super privilège
  2. la valeur de max_connections
  3. 2 x esclave de réplication
4
Dimitre Radoulov

La meilleure façon de voir les threads actuels (y compris tous les threads de premier plan et d'arrière-plan) est maintenant avec performance_schema. Par exemple:

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)

Je crois que les threads d'arrière-plan sont fixes en nombre (le nombre réel dépendra de certains paramètres de configuration tels que innodb_read_io_threads et le nombre de innodb_purge_threads etc.) Les threads FOREGROUND sont un par connexion + un potentiel de thread_cache_size en plus de cela.

6
Morgan Tocker