MySQL 5.7 – Understanding Thread in thread_cache_size, Thread_connected, and thread_created

cacheconnectionsMySQLmysql-5.7

In the MySQL 5.7 manual, I read that the thread cache has a size determined by the thread_cache_size system variable.

The default value is 0 (no caching), which causes a thread to be set up for each new connection and disposed of when the connection terminates.

But what exactly is a "thread"?

A connection thread becomes inactive when the client connection with which it was associated terminates.

Again, I can't understand what is a "thread" that I'm reading about.

Threads_created: The number of threads created to handle connections.

What the word "thread" refers to in this case?

Threads_cached: The number of threads in the thread cache.

Again this word "thread".

Threads_connected: The number of currently open connections.

I can't understand what is a thread. It is not connection I guess…

But what is it? Maybe it's only a matter of translation in my language, but I can't make up my mind to understand what is a "thread"

Best Answer

I believe in all the cases you mention the word "thread" simply means a computational thread, in the sense that is well described in this Wikipedia article, for example.

Leaving cache aside and starting from 0 connections, each new connection will cause a new processing thread to be created for that connection exclusive use; the thread will perform all SQL processing and network communication for this specific connected client. Since each thread is scheduled independently by the operating system, they appear to (or indeed do, when multiple processor cores are available) run concurrently.

Since creating a thread has some latency, it's often beneficial to have a pool of threads created in advance; each thread is still assigned to a connection exclusively, but when the connection is closed, the thread is not destroyed but rather returned to the pool to wait for another incoming connection.

You can find a detailed discussion of MySQL thread management in this book.