show processlist 详解

641 阅读9分钟

1. SHOW PROCESSLIST 语句

show processlist 其实查询的是 information_schema.processlist
show processlist 不可以接 where 过滤,information_schema.processlist 表可以

mysql> show processlist ;
+---------+-------------+---------------------+--------------------+---------+------+--------------+------------------------------------------------------------------------------------------------------+
| Id      | User        | Host                | db                 | Command | Time | State        | Info                                                                                                 |
+---------+-------------+---------------------+--------------------+---------+------+--------------+------------------------------------------------------------------------------------------------------+
| 9801429 | lis         | 10.41.7.10:57962    | lis                | Query   |    8 | Sending data | SELECT r.RgtantMobile, r.RgtantName, r.RgtNo, ag.SumGetMoney, ag.EnterAccDate, ag.BankAccNo, ( SELEC |
| 9802020 | ruihua      | 10.41.5.6:37543     | sales_org          | Sleep   |  292 |              | NULL                                                                                                 |
| 9802070 | lis         | 10.41.7.10:58998    | lis                | Query   |    8 | Sending data | select distinct d.contno,e.phone,d.appntidtype,d.appntidno,d.appntname,d.appntsex ,d.AppntBirthday,( |
| 9802084 | evoiceadmin | 10.41.8.8:41868     | evoicerh           | Sleep   |   57 |              | NULL                                                                                                 |
| 9802201 | root        | 10.41.100.3:38976   | NULL               | Query   |    0 | init         | show processlist                                                                                     |
+---------+-------------+---------------------+--------------------+---------+------+--------------+------------------------------------------------------------------------------------------------------+
148 rows in set (0.00 sec)

2. INFORMATION_SCHEMA PROCESSLIST 表

PROCESSLIST 表有如下列:

  • ID

    唯一连接标识。SHOW PROCESSLIST语句所示Id的值,performance_schema.threadsPROCESSLIST_ID的值,mysql 线程 CONNECTION_ID() 函数返回的值,与此值是一样的。

  • USER

    提交该语句的 MySQL 用户。 值为 system user 代表是服务器内部执行任务的无客户端线程,比如,一个 delayed-row 处理线程或者主从复制的一个 I/O or SQL 线程。对于 system user,在 Host column不指定任何Host值。值为unauthenticated user 代表已经建立了连接但是客户端用户还没有认证通过的线程。值为 event_scheduler 代表监控 scheduled events 的线程。(参考 Section 20.4, “Using the Event Scheduler”)。

  • HOST

    提交语句的客户端的 host name ( system user 没有 host)。TPC/IP连接的host name以 *host_name*:*client_port* 的格式显示,以便于确认是哪个客户端在操作。

  • DB

    线程的默认数据库,如果没有则是 NULL

  • COMMAND

    线程根据客户端行为正在执行的命令的类型,或是 Sleep如果连接空置。对于线程命令的描述,参考Section 8.14, “Examining Server Thread (Process) Information”。该列的值与客户端/服务器协议的 COM_*xxx*命令和Com_*xxx*状态变量相应。参考Section 5.1.9, “Server Status Variables”

  • TIME

    线程处于当前状态的时间,以秒计。对于SQL复制线程,值为最后复制事件的时间戳和复制服务器的真实时间的差,以秒计。参考Section 17.2.1, “Replication Threads”

  • STATE

    描述线程正在做什么的 action, event, or state。 关于 STATE值的描述,参考Section 8.14, “Examining Server Thread (Process) Information”.

    大多数状态都对应的很快的操作。如果一个线程在一个状态保持了很长时间,那么就是值得调查的问题了。

  • INFO

    线程正在执行的语句,如果没有执行则为 NULL 。语句可以是客户端发送到服务器的,也可以是某语句执行其他语句的内部语句。 比如,一个 CALL 语句调用了一个执行 SELECT 语句的存储过程, INFO 值会显示该SELECT 语句。

3. MySQL数据库 Thread (Process) 信息

关于如何查看线程信息

4. Thread Command Values

线程对应 Command 值的清单:

image.png

QuerySleep 常见
Time 不再用了

5. General Thread States

如下清单描述了与常规查询处理相关的线程State值,其他的特殊活动(比如复制)不在此列。 其中的很多状态只对查找服务器bug有用。

  • After create

    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.

  • altering table

    The server is in the process of executing an in-place ALTER TABLE.

  • Analyzing

    The thread is calculating a MyISAM table key distributions (for example, for ANALYZE TABLE).

  • checking permissions

    The thread is checking whether the server has the required privileges to execute the statement.

  • Checking table

    The thread is performing a table check operation.

  • cleaning up

    The thread has processed one command and is preparing to free memory and reset certain state variables.

  • closing tables

    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.

  • committing alter table to storage engine

    The server has finished an in-place ALTER TABLE and is committing the result.

  • converting HEAP to MyISAM

    The thread is converting an internal temporary table from a MEMORY table to an on-disk MyISAM table.

  • copy to tmp 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.

  • Copying to group table

    If a statement has different ORDER BY and GROUP BY criteria, the rows are sorted by group and copied to a temporary table.

  • Copying to tmp table

    The server is copying to a temporary table in memory.

  • Copying to tmp table on disk

    The server is copying to a temporary table on disk. The temporary result set has become too large (see Section 8.4.4, “Internal Temporary Table Use in MySQL”). Consequently, the thread is changing the temporary table from in-memory to disk-based format to save memory.

  • Creating index

    The thread is processing ALTER TABLE ... ENABLE KEYS for a MyISAM table.

  • Creating sort index

    The thread is processing a SELECT that is resolved using an internal temporary table.

  • creating table

    The thread is creating a table. This includes creation of temporary tables.

  • Creating tmp table

    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.

  • deleting from main table

    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.

  • discard_or_import_tablespace

    The thread is processing an ALTER TABLE ... DISCARD TABLESPACE or ALTER TABLE ... IMPORT TABLESPACE statement.

  • end

    This occurs at the end but before the cleanup of ALTER TABLE, CREATE VIEW, DELETE, INSERT, SELECT, or UPDATE statements.

    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
  • executing

    The thread has begun executing a statement.

  • Execution of init_command

    The thread is executing statements in the value of the init_command system variable.

  • freeing items

    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.

  • FULLTEXT initialization

    The server is preparing to perform a natural-language full-text search.

  • init

    This occurs before the initialization of ALTER TABLE, DELETE, INSERT, SELECT, or UPDATE statements. Actions taken by the server in this state include flushing the binary log, the InnoDB log, and some query cache cleanup operations.

  • Killed

    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.

  • logging slow query

    The thread is writing a statement to the slow-query log.

  • login

    The initial state for a connection thread until the client has been authenticated successfully.

  • manage keys

    The server is enabling or disabling a table index.

  • Opening tables

    线程正在准备打开一张表。这应该是一个非常快的过程,除非某些因素阻止了打开。比如,一个 ALTER TABLE 或 一个 LOCK TABLE 语句会阻止打开表直到该语句结束。检查table_open_cache 值是否够大也是很有意义的。

  • optimizing

    The server is performing initial optimizations for a query.

  • preparing

    This state occurs during query optimization.

  • preparing for alter table

    The server is preparing to execute an in-place ALTER TABLE.

  • Purging old relay logs

    The thread is removing unneeded relay log files.

  • query end

    This state occurs after processing a query but before the freeing items state.

  • Reading from net

    The server is reading a packet from the network.

  • Removing duplicates

    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.

  • removing tmp table

    The thread is removing an internal temporary table after processing a SELECT statement. This state is not used if no temporary table was created.

  • rename

    The thread is renaming a table.

  • rename result table

    The thread is processing an ALTER TABLE statement, has created the new table, and is renaming it to replace the original table.

  • Reopen tables

    线程获取到该表的锁,但是获取后底层表结构变更了。它已经释放锁、关闭了表,正在重新打开该表。

  • Repair by sorting

    The repair code is using a sort to create indexes.

  • Repair done

    The thread has completed a multithreaded repair for a MyISAM table.

  • Repair with keycache

    The repair code is using creating keys one by one through the key cache. This is much slower than Repair by sorting.

  • Rolling back

    The thread is rolling back a transaction.

  • Saving state

    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, the AUTO_INCREMENT counter, and key distributions.

  • Searching rows for update

    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

    线程正在给一条SELECT 语句读取和处理行,然后发送给客户端。因为在这个状态下的操作往往需要大量的磁盘访问(读),因此它通常是查询生命周期内最长的一个运行状态。

  • setup

    The thread is beginning an ALTER TABLE operation.

  • Sorting for group

    The thread is doing a sort to satisfy a GROUP BY.

  • Sorting for order

    The thread is doing a sort to satisfy an ORDER BY.

  • Sorting index

    The thread is sorting index pages for more efficient access during a MyISAM table optimization operation.

  • Sorting result

    For a SELECT statement, this is similar to Creating sort index, but for nontemporary tables.

  • statistics

    服务器正在计算统计信息以确定查询执行计划。如果一个线程在这个状态很长时间了,说明其他工作占用了磁盘。

  • System lock

    线程已经调用了 mysql_lock_tables() 但是线程状态还没有被更新。这是个常见状态,有数个因素都会导致。

    比如,进程正在请求或者等待表的内部锁或外部锁。当InnoDB 在执行 LOCK TABLES的时候等待表级锁,就会发生。 如果是因为请求外部锁,并且没有多个 mysqld servers访问同一个MyISAM 表,你可以使用 --skip-external-locking 参数关闭外部锁。但是,外部锁默认是关闭的,所以这个参数通常没意义。 SHOW PROFILE,这个状态意味着线程正在请求锁(而非等待)。

  • update

    The thread is getting ready to start updating the table.

  • Updating

    The thread is searching for rows to update and is updating them.

  • updating main table

    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.

  • updating reference tables

    The server is executing the second part of a multiple-table update and updating the matched rows from the other tables.

  • User lock

    线程正在请求或者等待GET_LOCK() 调用的advisory lock。. 对于 SHOW PROFILE,这个状态意味着线程正在请求锁(而非等待)。

  • User sleep

    The thread has invoked a SLEEP() call.

  • Waiting for commit lock

    FLUSH TABLES WITH READ LOCK 正在等待commit锁。

  • Waiting for global read lock

    FLUSH TABLES WITH READ LOCK 正在等待全局读锁,或者全局 read_only 系统变量被设置了。

  • Waiting for tables

    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, or OPTIMIZE TABLE.

  • Waiting for table flush

    The thread is executing FLUSH TABLES and is waiting for all threads to close their tables, or 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, or OPTIMIZE TABLE.

  • Waiting for *lock_type* lock

    服务器正在等待获取THR_LOCK锁,或元数据锁,*lock_type*描述了锁的类型。

    如下状态表示在等待THR_LOCK锁:

    • Waiting for table level lock

    如下状态表示在等待元数据锁:

    • Waiting for event metadata lock
    • Waiting for global read lock
    • Waiting for schema metadata lock
    • Waiting for stored function metadata lock
    • Waiting for stored procedure metadata lock
    • Waiting for table metadata lock
    • Waiting for trigger metadata lock

    更多关于表锁的描述,参见Section 8.11.1, “Internal Locking Methods”
    更多关于元数据锁的描述,参见 Section 8.11.4, “Metadata Locking”

  • Waiting on cond

    A generic state in which the thread is waiting for a condition to become true. No specific state information is available.

  • Writing to net

    The server is writing a packet to the network.