mysql中的 show processlist 命令是MySQL数据库中运用最多的命令,通过该命理可以查看到mysql所的进程状态,所以对show processlist 的状态进行理解和确认,也就显得非常必要了。
MySQL官方文档中,对于show processlist的说明如下:
http://dev.mysql.com/doc/refman/5.5/en/show-processlist.html
SHOW PROCESSLIST Syntax
SHOW [FULL] PROCESSLIST
SHOW PROCESSLIST
shows you which threads are running. You can also get this information from theINFORMATION_SCHEMA
PROCESSLIST
table or the mysqladmin processlist command. If you have the PROCESS
privilege, you can see all threads. Otherwise, you can see only your own threads (that is, threads associated with the MySQL account that you are using). If you do not use the FULL
keyword, only the first 100 characters of each statement are shown in the Info
field.
This statement is very useful if you get the “too many connections” error message and want to find out what is going on. MySQL reserves one extra connection to be used by accounts that have the SUPER
privilege, to ensure that administrators should always be able to connect and check the system (assuming that you are not giving this privilege to all your users).
Threads can be killed with the KILL
statement. See Section 13.7.6.4, “KILL Syntax”.
Here is an example of SHOW PROCESSLIST
output:
mysql> SHOW FULL PROCESSLIST\G *************************** 1. row *************************** Id: 1 User: system user Host: db: NULL Command: Connect Time: 1030455 State: Waiting for master to send event Info: NULL *************************** 2. row *************************** Id: 2 User: system user Host: db: NULL Command: Connect Time: 1004 State: Has read all relay log; waiting for the slave I/O thread to update it Info: NULL *************************** 3. row *************************** Id: 3112 User: replikator Host: artemis:2204 db: NULL Command: Binlog Dump Time: 2144 State: Has sent all binlog to slave; waiting for binlog to be updated Info: NULL *************************** 4. row *************************** Id: 3113 User: replikator Host: iconnect2:45781 db: NULL Command: Binlog Dump Time: 2086 State: Has sent all binlog to slave; waiting for binlog to be updated Info: NULL *************************** 5. row *************************** Id: 3123 User: stefan Host: localhost db: apollon Command: Query Time: 0 State: NULL Info: SHOW FULL PROCESSLIST 5 rows in set (0.00 sec)
The columns produced by SHOW PROCESSLIST
have the following meanings:
-
The connection identifier. This is the same type of value displayed in the
ID
column of theINFORMATION_SCHEMA.PROCESSLIST
table, thePROCESSLIST_ID
column of the Performance Schemathreads
table, and returned by theCONNECTION_ID()
function. -
The MySQL user who issued the statement. If this is
system user
, it refers to a nonclient thread spawned by the server to handle tasks internally. This could be the I/O or SQL thread used on replication slaves or a delayed-row handler.unauthenticated user
refers to a thread that has become associated with a client connection but for which authentication of the client user has not yet been done.event_scheduler
refers to the thread that monitors scheduled events. Forsystem user
, there is no host specified in theHost
column. -
The host name of the client issuing the statement (except for
system user
where there is no host).SHOW PROCESSLIST
reports the host name for TCP/IP connections in
format to make it easier to determine which client is doing what.host_name
:client_port
-
The default database, if one is selected, otherwise
NULL
. -
The type of command the thread is executing. For descriptions for thread commands, see Section 8.12.5, “Examining Thread Information”. The value of this column corresponds to the
COM_
commands of the client/server protocol andxxx
Com_
status variables. See Section 5.1.6, “Server Status Variables”xxx
-
The time in seconds that the thread has been in its current state. For a slave SQL thread, the value is the number of seconds between the timestamp of the last replicated event and the real time of the slave machine. SeeSection 17.2.1, “Replication Implementation Details”.
-
An action, event, or state that indicates what the thread is doing. Descriptions for
State
values can be found atSection 8.12.5, “Examining Thread Information”.Most states correspond to very quick operations. If a thread stays in a given state for many seconds, there might be a problem that needs to be investigated.
For the
SHOW PROCESSLIST
statement, the value ofState
isNULL
. -
The statement the thread is executing, or
NULL
if it is not executing any statement. The statement might be the one sent to the server, or an innermost statement if the statement executes other statements. For example, if aCALL
statement executes a stored procedure that is executing aSELECT
statement, theInfo
value shows theSELECT
statement.
对于其中 state 的状态值,有下面的范围:
http://dev.mysql.com/doc/refman/5.5/en/general-thread-states.html
General Thread States
The following list describes thread State
values that are associated with general query processing and not more specialized activities such as replication. Many of these are useful only for finding bugs in the server.
-
This occurs when the thread creates a table (including internal temporary tables), at the end of the function that creates the table. This state is used even if the table could not be created due to some error.
-
The thread is calculating a
MyISAM
table key distributions (for example, forANALYZE TABLE
). -
The thread is checking whether the server has the required privileges to execute the statement.
-
The thread is performing a table check operation.
-
The thread has processed one command and is preparing to free memory and reset certain state variables.
-
The thread is flushing the changed table data to disk and closing the used tables. This should be a fast operation. If not, verify that you do not have a full disk and that the disk is not in very heavy use.
-
The thread is converting an internal temporary table from a
MEMORY
table to an on-diskMyISAM
table. -
The thread is processing an
ALTER TABLE
statement. This state occurs after the table with the new structure has been created but before rows are copied into it. -
If a statement has different
ORDER BY
andGROUP BY
criteria, the rows are sorted by group and copied to a temporary table. -
The server is copying to a temporary table in memory.
-
The server is copying to a temporary table on disk. The temporary result set has become too large (seeSection 8.4.4, “How MySQL Uses Internal Temporary Tables”). Consequently, the thread is changing the temporary table from in-memory to disk-based format to save memory.
-
The thread is processing
ALTER TABLE ... ENABLE KEYS
for aMyISAM
table. -
The thread is processing a
SELECT
that is resolved using an internal temporary table. -
The thread is creating a table. This includes creation of temporary tables.
-
The thread is creating a temporary table in memory or on disk. If the table is created in memory but later is converted to an on-disk table, the state during that operation will be
Copying to tmp table on disk
. -
The server is executing the first part of a multiple-table delete. It is deleting only from the first table, and saving columns and offsets to be used for deleting from the other (reference) tables.
-
deleting from reference tables
The server is executing the second part of a multiple-table delete and deleting the matched rows from the other tables.
-
The thread is processing an
ALTER TABLE ... DISCARD TABLESPACE
orALTER TABLE ... IMPORT TABLESPACE
statement. -
This occurs at the end but before the cleanup of
ALTER TABLE
,CREATE VIEW
,DELETE
,INSERT
,SELECT
, orUPDATE
statements. -
The thread has begun executing a statement.
-
The thread is executing statements in the value of the
init_command
system variable. -
The thread has executed a command. Some freeing of items done during this state involves the query cache. This state is usually followed by
cleaning up
. -
The thread is executing
FLUSH TABLES
and is waiting for all threads to close their tables. -
The server is preparing to perform a natural-language full-text search.
-
This occurs before the initialization of
ALTER TABLE
,DELETE
,INSERT
,SELECT
, orUPDATE
statements. Actions taken by the server in this state include flushing the binary log, theInnoDB
log, and some query cache cleanup operations.For the
end
state, the following operations could be happening:-
Removing query cache entries after data in a table is changed
-
Writing an event to the binary log
-
Freeing memory buffers, including for blobs
-
-
Someone has sent a
KILL
statement to the thread and it should abort next time it checks the kill flag. The flag is checked in each major loop in MySQL, but in some cases it might still take a short time for the thread to die. If the thread is locked by some other thread, the kill takes effect as soon as the other thread releases its lock. -
The query is locked by another query.
As of MySQL 5.5.3, this state was removed because it was equivalent to the
Table lock
state and no longer appears inSHOW PROCESSLIST
output. -
The thread is writing a statement to the slow-query log.
-
This state is used for the
SHOW PROCESSLIST
state. -
The initial state for a connection thread until the client has been authenticated successfully.
-
The server is enabling or disabling a table index.
-
The thread is trying to open a table. This is should be very fast procedure, unless something prevents opening. For example, an
ALTER TABLE
or aLOCK TABLE
statement can prevent opening a table until the statement is finished. It is also worth checking that yourtable_open_cache
value is large enough. -
The server is performing initial optimizations for a query.
-
This state occurs during query optimization.
-
The thread is removing unneeded relay log files.
-
This state occurs after processing a query but before the
freeing items
state. -
The server is reading a packet from the network.
-
The query was using
SELECT DISTINCT
in such a way that MySQL could not optimize away the distinct operation at an early stage. Because of this, MySQL requires an extra stage to remove all duplicated rows before sending the result to the client. -
The thread is removing an internal temporary table after processing a
SELECT
statement. This state is not used if no temporary table was created. -
The thread is renaming a table.
-
The thread is processing an
ALTER TABLE
statement, has created the new table, and is renaming it to replace the original table. -
The thread got a lock for the table, but noticed after getting the lock that the underlying table structure changed. It has freed the lock, closed the table, and is trying to reopen it.
-
The repair code is using a sort to create indexes.
-
The thread has completed a multi-threaded repair for a
MyISAM
table. -
The repair code is using creating keys one by one through the key cache. This is much slower than
Repair by sorting
. -
The thread is rolling back a transaction.
-
For
MyISAM
table operations such as repair or analysis, the thread is saving the new table state to the.MYI
file header. State includes information such as number of rows, theAUTO_INCREMENT
counter, and key distributions. -
The thread is doing a first phase to find all matching rows before updating them. This has to be done if the
UPDATE
is changing the index that is used to find the involved rows. -
Sending data
The thread is reading and processing rows for a
SELECT
statement, and sending data to the client. Because operations occurring during this state tend to perform large amounts of disk access (reads), it is often the longest-running state over the lifetime of a given query. -
The thread is beginning an
ALTER TABLE
operation. -
The thread is doing a sort to satisfy a
GROUP BY
. -
The thread is doing a sort to satisfy a
ORDER BY
. -
The thread is sorting index pages for more efficient access during a
MyISAM
table optimization operation. -
For a
SELECT
statement, this is similar toCreating sort index
, but for nontemporary tables. -
The server is calculating statistics to develop a query execution plan. If a thread is in this state for a long time, the server is probably disk-bound performing other work.
-
The thread is going to request or is waiting for an internal or external system lock for the table. If this state is being caused by requests for external locks and you are not using multiple mysqld servers that are accessing the same
MyISAM
tables, you can disable external system locks with the--skip-external-locking
option. However, external locking is disabled by default, so it is likely that this option will have no effect. ForSHOW PROFILE
, this state means the thread is requesting the lock (not waiting for it). -
The next thread state after
System lock
. The thread has acquired an external lock and is going to request an internal table lock.This state was replaced in MySQL 5.5.6 with
Waiting for table level lock
. -
The thread is getting ready to start updating the table.
-
The thread is searching for rows to update and is updating them.
-
The server is executing the first part of a multiple-table update. It is updating only the first table, and saving columns and offsets to be used for updating the other (reference) tables.
-
The server is executing the second part of a multiple-table update and updating the matched rows from the other tables.
-
The thread is going to request or is waiting for an advisory lock requested with a
GET_LOCK()
call. ForSHOW PROFILE
, this state means the thread is requesting the lock (not waiting for it). -
The thread has invoked a
SLEEP()
call. -
Waiting for all running commits to finish
A statement that causes an explicit or implicit commit is waiting for release of a read lock. This state was removed in MySQL 5.5.8;
Waiting for commit lock
is used instead. -
A statement that causes an explicit or implicit commit is waiting for release of a read lock or
FLUSH TABLES WITH READ LOCK
is waiting for a commit lock. This state was added in MySQL 5.5.8. -
FLUSH TABLES WITH READ LOCK
is waiting for a global read lock or the globalread_only
system variable is being set. -
Waiting for release of readlock
The thread is waiting for a global read lock obtained by another thread (with
FLUSH TABLES WITH READ LOCK
) to be released. This state was removed in MySQL 5.5.8;Waiting for global read lock
orWaiting for commit lock
are used instead. -
Waiting for tables
,Waiting for table
,Waiting for table flush
The thread got a notification that the underlying structure for a table has changed and it needs to reopen the table to get the new structure. However, to reopen the table, it must wait until all other threads have closed the table in question.
This notification takes place if another thread has used
FLUSH TABLES
or one of the following statements on the table in question:FLUSH TABLES
,tbl_name
ALTER TABLE
,RENAME TABLE
,REPAIR TABLE
,ANALYZE TABLE
, orOPTIMIZE TABLE
.In MySQL 5.5.6,
Waiting for table
was replaced withWaiting for table flush
. -
The server is waiting to acquire a lock, where
lock_type
indicates the type of lock:-
Waiting for event metadata lock
(added in MySQL 5.5.8) -
Waiting for global metadata lock
(replaced byWaiting for global read lock
in MySQL 5.5.8) -
Waiting for global read lock
(added in MySQL 5.5.8) -
Waiting for schema metadata lock
-
Waiting for stored function metadata lock
-
Waiting for stored procedure metadata lock
-
Waiting for table level lock
-
Waiting for table metadata lock
-
Waiting for trigger metadata lock
(added in MySQL 5.5.8)
-
-
A generic state in which the thread is waiting for a condition to become true. No specific state information is available.
-
The thread has issued a
FLUSH TABLES WITH READ LOCK
statement to obtain a global read lock and is waiting to obtain the lock. This state was removed in MySQL 5.5.8;Waiting for global read lock
is used instead. -
The server is writing a packet to the network.