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.threads表PROCESSLIST_ID的值,mysql 线程CONNECTION_ID()函数返回的值,与此值是一样的。 -
USER提交该语句的 MySQL 用户。 值为
system user代表是服务器内部执行任务的无客户端线程,比如,一个 delayed-row 处理线程或者主从复制的一个 I/O or SQL 线程。对于system user,在Hostcolumn不指定任何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 值的清单:
Query,Sleep 常见
Time 不再用了
5. General Thread States
如下清单描述了与常规查询处理相关的线程State值,其他的特殊活动(比如复制)不在此列。 其中的很多状态只对查找服务器bug有用。
-
After createThis 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 tableThe server is in the process of executing an in-place
ALTER TABLE. -
AnalyzingThe thread is calculating a
MyISAMtable key distributions (for example, forANALYZE TABLE). -
checking permissionsThe thread is checking whether the server has the required privileges to execute the statement.
-
Checking tableThe thread is performing a table check operation.
-
cleaning upThe thread has processed one command and is preparing to free memory and reset certain state variables.
-
closing tablesThe 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 engineThe server has finished an in-place
ALTER TABLEand is committing the result. -
converting HEAP to MyISAMThe thread is converting an internal temporary table from a
MEMORYtable to an on-diskMyISAMtable. -
copy to tmp tableThe thread is processing an
ALTER TABLEstatement. This state occurs after the table with the new structure has been created but before rows are copied into it. -
Copying to group tableIf a statement has different
ORDER BYandGROUP BYcriteria, the rows are sorted by group and copied to a temporary table. -
Copying to tmp tableThe server is copying to a temporary table in memory.
-
Copying to tmp table on diskThe 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 indexThe thread is processing
ALTER TABLE ... ENABLE KEYSfor aMyISAMtable. -
Creating sort indexThe thread is processing a
SELECTthat is resolved using an internal temporary table. -
creating tableThe thread is creating a table. This includes creation of temporary tables.
-
Creating tmp tableThe 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 tableThe 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 tablesThe server is executing the second part of a multiple-table delete and deleting the matched rows from the other tables.
-
discard_or_import_tablespaceThe thread is processing an
ALTER TABLE ... DISCARD TABLESPACEorALTER TABLE ... IMPORT TABLESPACEstatement. -
endThis occurs at the end but before the cleanup of
ALTER TABLE,CREATE VIEW,DELETE,INSERT,SELECT, orUPDATEstatements.For the
endstate, 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
-
executingThe thread has begun executing a statement.
-
Execution of init_commandThe thread is executing statements in the value of the
init_commandsystem variable. -
freeing itemsThe 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 initializationThe server is preparing to perform a natural-language full-text search.
-
initThis occurs before the initialization of
ALTER TABLE,DELETE,INSERT,SELECT, orUPDATEstatements. Actions taken by the server in this state include flushing the binary log, theInnoDBlog, and some query cache cleanup operations. -
KilledSomeone has sent a
KILLstatement 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 queryThe thread is writing a statement to the slow-query log.
-
loginThe initial state for a connection thread until the client has been authenticated successfully.
-
manage keysThe server is enabling or disabling a table index.
-
Opening tables线程正在准备打开一张表。这应该是一个非常快的过程,除非某些因素阻止了打开。比如,一个
ALTER TABLE或 一个LOCK TABLE语句会阻止打开表直到该语句结束。检查table_open_cache值是否够大也是很有意义的。 -
optimizingThe server is performing initial optimizations for a query.
-
preparingThis state occurs during query optimization.
-
preparing for alter tableThe server is preparing to execute an in-place
ALTER TABLE. -
Purging old relay logsThe thread is removing unneeded relay log files.
-
query endThis state occurs after processing a query but before the
freeing itemsstate. -
Reading from netThe server is reading a packet from the network.
-
Removing duplicatesThe query was using
SELECT DISTINCTin 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 tableThe thread is removing an internal temporary table after processing a
SELECTstatement. This state is not used if no temporary table was created. -
renameThe thread is renaming a table.
-
rename result tableThe thread is processing an
ALTER TABLEstatement, has created the new table, and is renaming it to replace the original table. -
Reopen tables线程获取到该表的锁,但是获取后底层表结构变更了。它已经释放锁、关闭了表,正在重新打开该表。
-
Repair by sortingThe repair code is using a sort to create indexes.
-
Repair doneThe thread has completed a multithreaded repair for a
MyISAMtable. -
Repair with keycacheThe repair code is using creating keys one by one through the key cache. This is much slower than
Repair by sorting. -
Rolling backThe thread is rolling back a transaction.
-
Saving stateFor
MyISAMtable operations such as repair or analysis, the thread is saving the new table state to the.MYIfile header. State includes information such as number of rows, theAUTO_INCREMENTcounter, and key distributions. -
Searching rows for updateThe thread is doing a first phase to find all matching rows before updating them. This has to be done if the
UPDATEis changing the index that is used to find the involved rows. -
Sending data线程正在给一条
SELECT语句读取和处理行,然后发送给客户端。因为在这个状态下的操作往往需要大量的磁盘访问(读),因此它通常是查询生命周期内最长的一个运行状态。 -
setupThe thread is beginning an
ALTER TABLEoperation. -
Sorting for groupThe thread is doing a sort to satisfy a
GROUP BY. -
Sorting for orderThe thread is doing a sort to satisfy an
ORDER BY. -
Sorting indexThe thread is sorting index pages for more efficient access during a
MyISAMtable optimization operation. -
Sorting resultFor a
SELECTstatement, this is similar toCreating sort index, but for nontemporary tables. -
statistics服务器正在计算统计信息以确定查询执行计划。如果一个线程在这个状态很长时间了,说明其他工作占用了磁盘。
-
System lock线程已经调用了
mysql_lock_tables()但是线程状态还没有被更新。这是个常见状态,有数个因素都会导致。比如,进程正在请求或者等待表的内部锁或外部锁。当
InnoDB在执行LOCK TABLES的时候等待表级锁,就会发生。 如果是因为请求外部锁,并且没有多个 mysqld servers访问同一个MyISAM表,你可以使用--skip-external-locking参数关闭外部锁。但是,外部锁默认是关闭的,所以这个参数通常没意义。SHOW PROFILE,这个状态意味着线程正在请求锁(而非等待)。 -
updateThe thread is getting ready to start updating the table.
-
UpdatingThe thread is searching for rows to update and is updating them.
-
updating main tableThe 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 tablesThe 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 sleepThe thread has invoked a
SLEEP()call. -
Waiting for commit lockFLUSH TABLES WITH READ LOCK正在等待commit锁。 -
Waiting for global read lockFLUSH TABLES WITH READ LOCK正在等待全局读锁,或者全局read_only系统变量被设置了。 -
Waiting for tablesThe 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 TABLESor one of the following statements on the table in question:FLUSH TABLES *tbl_name*,ALTER TABLE,RENAME TABLE,REPAIR TABLE,ANALYZE TABLE, orOPTIMIZE TABLE. -
Waiting for table flushThe thread is executing
FLUSH TABLESand 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 TABLESor one of the following statements on the table in question:FLUSH TABLES *tbl_name*,ALTER TABLE,RENAME TABLE,REPAIR TABLE,ANALYZE TABLE, orOPTIMIZE TABLE. -
Waiting for *lock_type* lock服务器正在等待获取
THR_LOCK锁,或元数据锁,*lock_type*描述了锁的类型。如下状态表示在等待
THR_LOCK锁:Waiting for table level lock
如下状态表示在等待元数据锁:
Waiting for event metadata lockWaiting for global read lockWaiting for schema metadata lockWaiting for stored function metadata lockWaiting for stored procedure metadata lockWaiting for table metadata lockWaiting for trigger metadata lock
更多关于表锁的描述,参见Section 8.11.1, “Internal Locking Methods”。
更多关于元数据锁的描述,参见 Section 8.11.4, “Metadata Locking”。 -
Waiting on condA generic state in which the thread is waiting for a condition to become true. No specific state information is available.
-
Writing to netThe server is writing a packet to the network.