MySQL EXPLAIN

explain显示了mysql如何使用索引来处理select语句以及连接表。可以帮助选择更好的索引和写出更优化的查询语句。

使用方法,在select语句前加上explain就可以了:

如:

  1. explain select surname,first_name form a,b where a.id=b.id

EXPLAIN列的解释:

table:显示这一行的数据是关于哪张表的

type:这是重要的列,显示连接使用了何种类型。从最好到最差的连接类型为const、eq_reg、ref、range、indexhe和ALL

possible_keys:显示可能应用在这张表中的索引。如果为空,没有可能的索引。可以为相关的域从WHERE语句中选择一个合适的语句

key: 实际使用的索引。如果为NULL,则没有使用索引。很少的情况下,MYSQL会选择优化不足的索引。这种情况下,可以在SELECT语句中使用USE INDEX(indexname)来强制使用一个索引或者用IGNORE INDEX(indexname)来强制MYSQL忽略索引

key_len:使用的索引的长度。在不损失精确性的情况下,长度越短越好

ref:显示索引的哪一列被使用了,如果可能的话,是一个常数

rows:MYSQL认为必须检查的用来返回请求数据的行数

Extra:关于MYSQL如何解析查询的额外信息。将在表4.3中讨论,但这里可以看到的坏的例子是Using temporary和Using filesort,意思MYSQL根本不能使用索引,结果是检索会很慢

extra列返回的描述的意义

Distinct:一旦MYSQL找到了与行相联合匹配的行,就不再搜索了

Not exists: MYSQL优化了LEFT JOIN,一旦它找到了匹配LEFT JOIN标准的行,就不再搜索了

Range checked for each Record(index map:#):没有找到理想的索引,因此对于从前面表中来的每一个行组合,MYSQL检查使用哪个索引,并用它来从表中返回行。这是使用索引的最慢的连接之一

Using filesort: 看到这个的时候,查询就需要优化了。MYSQL需要进行额外的步骤来发现如何对返回的行排序。它根据连接类型以及存储排序键值和匹配条件的全部行的行指针来排序全部行

Using index: 列数据是从仅仅使用了索引中的信息而没有读取实际的行动的表返回的,这发生在对表的全部的请求列都是同一个索引的部分的时候

Using temporary 看到这个的时候,查询需要优化了。这里,MYSQL需要创建一个临时表来存储结果,这通常发生在对不同的列集进行ORDER BY上,而不是GROUP BY上

Where used 使用了WHERE从句来限制哪些行将与下一张表匹配或者是返回给用户。如果不想返回表中的全部行,并且连接类型ALL或index,这就会发生,或者是查询有问题不同连接类型的解释(按照效率高低的顺序排序)

system 表只有一行:system表。这是const连接类型的特殊情况

const:表中的一个记录的最大值能够匹配这个查询(索引可以是主键或惟一索引)。因为只有一行,这个值实际就是常数,因为MYSQL先读这个值然后把它当做常数来对待

eq_ref:在连接中,MYSQL在查询时,从前面的表中,对每一个记录的联合都从表中读取一个记录,它在查询使用了索引为主键或惟一键的全部时使用

ref:这个连接类型只有在查询使用了不是惟一或主键的键或者是这些类型的部分(比如,利用最左边前缀)时发生。对于之前的表的每一个行联合,全部记录都将从表中读出。这个类型严重依赖于根据索引匹配的记录多少—越少越好

range:这个连接类型使用索引返回一个范围中的行,比如使用>或<查找东西时发生的情况

index: 这个连接类型对前面的表中的每一个记录联合进行完全扫描(比ALL更好,因为索引一般小于表数据)

ALL:这个连接类型对于前面的每一个记录联合进行完全扫描,这一般比较糟糕,应该尽量避免

mysql> explain select * from user where user=’root’\G

*************************** 1. row ***************************

id: 1

select_type: SIMPLE

table: user

type: ALL

possible_keys: NULL

key: NULL

key_len: NULL

ref: NULL

rows: 5

Extra: Using where

1 row in set (0.00 sec)

SHOW PROCESSLIST介绍和使用

processlist命令的输出结果显示了有哪些线程在运行,可以帮助识别出有问题的查询语句,两种方式使用这个命令。
1.      进入mysql/bin目录下输入mysqladmin processlist;
2.      启动mysql,输入show processlist;
如果有SUPER权限,则可以看到全部的线程,否则,只能看到自己发起的线程(这是指,当前对应的MySQL帐户运行的线程)。

mysql> show processlist

-> ;

+—-+——-+———————-+——+———+——+——-+——————+

| Id | User  | Host                 | db   | Command | Time | State | Info             |

+—-+——-+———————-+——+———+——+——-+——————+

| 34 | ucjmh | 192.168.56.101:54031 | NULL | Query   |    0 | NULL  | show processlist |

+—-+——-+———————-+——+———+——+——-+——————+

1 row in set (0.00 sec)

mysql> exit

Bye

[root@ucjmh ~]# mysql -uroot -poracle

Welcome to the MySQL monitor.  Commands end with ; or \g.

Your MySQL connection id is 35

Server version: 5.5.41-log Source distribution

Copyright (c) 2000, 2014, 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> show processlist;

+—-+——+———–+——+———+——+——-+——————+

| Id | User | Host      | db   | Command | Time | State | Info             |

+—-+——+———–+——+———+——+——-+——————+

| 35 | root | localhost | NULL | Query   |    0 | NULL  | show processlist |

+—-+——+———–+——+———+——+——-+——————+

1 row in set (0.00 sec)

各列的含义和用途:

id,标识会话id 类似于oracle中的sid,你要kill一个语句的时候很有用。

user列,显示单前用户,如果不是root,这个命令就只显示你权限范围内的sql语句。

host列,显示这个语句是从哪个ip的哪个端口上发出的。用来追踪出问题语句的用户。

db列,显示这个进程目前连接的是哪个数据库。

command列,显示当前连接的执行的命令,一般就是休眠(sleep),查询(query),连接(connect)。

time列,此这个状态持续的时间,单位是秒。

state列,显示使用当前连接的sql语句的状态,很重要的列,后续会有所有的状态的描述,请注意,state只是语句执行中的某一个状态,一个sql语句,已查询为例,可能需要经过copying to tmp table,Sorting result,Sending data等状态才可以完成,

info列,显示这个sql语句,因为长度有限,所以长的sql语句就显示不全,但是一个判断问题语句的重要依据。Checking table
正在检查数据表(这是自动的)。
Closing tables
正在将表中修改的数据刷新到磁盘中,同时正在关闭已经用完的表。这是一个很快的操作,如果不是这样的话,就应该确认磁盘空间是否已经满了或者磁盘是否正处于重负中。
Connect Out
复制从服务器正在连接主服务器。
Copying to tmp table on disk
由于临时结果集大于tmp_table_size,正在将临时表从内存存储转为磁盘存储以此节省内存。
Creating tmp table
正在创建临时表以存放部分查询结果。
deleting from main table
服务器正在执行多表删除中的第一部分,刚删除第一个表。
deleting from reference tables
服务器正在执行多表删除中的第二部分,正在删除其他表的记录。
Flushing tables
正在执行FLUSH TABLES,等待其他线程关闭数据表。
Killed
发送了一个kill请求给某线程,那么这个线程将会检查kill标志位,同时会放弃下一个kill请求。MySQL会在每次的主循环中检查kill标志位,不过有些情况下该线程可能会过一小段才能死掉。如果该线程程被其他线程锁住了,那么kill请求会在锁释放时马上生效。
Locked
被其他查询锁住了。
Sending data
正在处理Select查询的记录,同时正在把结果发送给客户端。
Sorting for group
正在为GROUP BY做排序。
Sorting for order
正在为ORDER BY做排序。
Opening tables
这个过程应该会很快,除非受到其他因素的干扰。例如,在执Alter TABLE或LOCK TABLE语句行完以前,数据表无法被其他线程打开。正尝试打开一个表。
Removing duplicates
正在执行一个Select DISTINCT方式的查询,但是MySQL无法在前一个阶段优化掉那些重复的记录。因此,MySQL需要再次去掉重复的记录,然后再把结果发送给客户端。
Reopen table
获得了对一个表的锁,但是必须在表结构修改之后才能获得这个锁。已经释放锁,关闭数据表,正尝试重新打开数据表。
Repair by sorting
修复指令正在排序以创建索引。
Repair with keycache
修复指令正在利用索引缓存一个一个地创建新索引。它会比Repair by sorting慢些。
Searching rows for update
正在讲符合条件的记录找出来以备更新。它必须在Update要修改相关的记录之前就完成了。
Sleeping
正在等待客户端发送新请求.
System lock
正在等待取得一个外部的系统锁。如果当前没有运行多个mysqld服务器同时请求同一个表,那么可以通过增加–skip-external-locking参数来禁止外部系统锁。
Upgrading lock
Insert DELAYED正在尝试取得一个锁表以插入新记录。
Updating
正在搜索匹配的记录,并且修改它们。
User Lock
正在等待GET_LOCK()。
Waiting for tables
该线程得到通知,数据表结构已经被修改了,需要重新打开数据表以取得新的结构。然后,为了能的重新打开数据表,必须等到所有其他线程关闭这个表。以下几种情况下会产生这个通知:FLUSH TABLES tbl_name, Alter TABLE, RENAME TABLE, REPAIR TABLE, ANALYZE TABLE,或OPTIMIZE TABLE。
waiting for handler insert
Insert DELAYED已经处理完了所有待处理的插入操作,正在等待新的请求。
大部分状态对应很快的操作,只要有一个线程保持同一个状态好几秒钟,那么可能是有问题发生了,需要检查一下。
这个命令中最关键的就是state列,mysql列出的状态主要有以下几种:

Checking table
正在检查数据表(这是自动的)。
Closing tables
正在将表中修改的数据刷新到磁盘中,同时正在关闭已经用完的表。这是一个很快的操作,如果不是这样的话,就应该确认磁盘空间是否已经满了或者磁盘是否正处于重负中。
Connect Out
复制从服务器正在连接主服务器。
Copying to tmp table on disk
由于临时结果集大于tmp_table_size,正在将临时表从内存存储转为磁盘存储以此节省内存。
Creating tmp table
正在创建临时表以存放部分查询结果。
deleting from main table
服务器正在执行多表删除中的第一部分,刚删除第一个表。
deleting from reference tables
服务器正在执行多表删除中的第二部分,正在删除其他表的记录。
Flushing tables
正在执行FLUSH TABLES,等待其他线程关闭数据表。
Killed
发送了一个kill请求给某线程,那么这个线程将会检查kill标志位,同时会放弃下一个kill请求。MySQL会在每次的主循环中检查kill标志位,不过有些情况下该线程可能会过一小段才能死掉。如果该线程程被其他线程锁住了,那么kill请求会在锁释放时马上生效。
Locked
被其他查询锁住了。
Sending data
正在处理Select查询的记录,同时正在把结果发送给客户端。
Sorting for group
正在为GROUP BY做排序。
Sorting for order
正在为ORDER BY做排序。
Opening tables
这个过程应该会很快,除非受到其他因素的干扰。例如,在执Alter TABLE或LOCK TABLE语句行完以前,数据表无法被其他线程打开。正尝试打开一个表。
Removing duplicates
正在执行一个Select DISTINCT方式的查询,但是MySQL无法在前一个阶段优化掉那些重复的记录。因此,MySQL需要再次去掉重复的记录,然后再把结果发送给客户端。
Reopen table
获得了对一个表的锁,但是必须在表结构修改之后才能获得这个锁。已经释放锁,关闭数据表,正尝试重新打开数据表。
Repair by sorting
修复指令正在排序以创建索引。
Repair with keycache
修复指令正在利用索引缓存一个一个地创建新索引。它会比Repair by sorting慢些。
Searching rows for update
正在讲符合条件的记录找出来以备更新。它必须在Update要修改相关的记录之前就完成了。
Sleeping
正在等待客户端发送新请求.
System lock
正在等待取得一个外部的系统锁。如果当前没有运行多个mysqld服务器同时请求同一个表,那么可以通过增加–skip-external-locking参数来禁止外部系统锁。
Upgrading lock
Insert DELAYED正在尝试取得一个锁表以插入新记录。
Updating
正在搜索匹配的记录,并且修改它们。
User Lock
正在等待GET_LOCK()。
Waiting for tables
该线程得到通知,数据表结构已经被修改了,需要重新打开数据表以取得新的结构。然后,为了能的重新打开数据表,必须等到所有其他线程关闭这个表。以下几种情况下会产生这个通知:FLUSH TABLES tbl_name, Alter TABLE, RENAME TABLE, REPAIR TABLE, ANALYZE TABLE,或OPTIMIZE TABLE。
waiting for handler insert
Insert DELAYED已经处理完了所有待处理的插入操作,正在等待新的请求。
大部分状态对应很快的操作,只要有一个线程保持同一个状态好几秒钟,那么可能是有问题发生了,需要检查一下。

在mysql的官方文档中列出了所有可能的状态。

8.14.2 General Thread States

  •  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.
  •  AnalyzingThe thread is calculating a MyISAM table 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, you should verify that you do not have a full disk and that the disk is not in very heavy use.
  •  converting HEAP to MyISAMThe thread is converting an internal temporary table from a MEMORY table to an on-disk MyISAM table.
  •  copy to tmp tableThe 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 tableIf 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 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, “How MySQL Uses Internal Temporary Tables”). 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 KEYS for a MyISAMtable.
  • Creating sort indexThe thread is processing a SELECT that 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 TABLESPACE or ALTER TABLE ... IMPORT TABLESPACE statement.
  •  endThis occurs at the end but before the cleanup of ALTER TABLECREATE VIEWDELETEINSERTSELECT, or UPDATE statements.
  •  executingThe thread has begun executing a statement.
  •  Execution of init_commandThe thread is executing statements in the value of the init_command system 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.
  •  Flushing tablesThe thread is executing FLUSH TABLES and is waiting for all threads to close their tables.
  •  FULLTEXT initializationThe server is preparing to perform a natural-language full-text search.
  •  initThis occurs before the initialization of ALTER TABLEDELETEINSERT,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.

    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
  •  KilledSomeone 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.
  •  LockedThe query is locked by another query.
  •  logging slow queryThe thread is writing a statement to the slow-query log.
  •  NULLThis state is used for the SHOW PROCESSLIST state.
  •  loginThe initial state for a connection thread until the client has been authenticated successfully.
  •  Opening tablesOpening tableThe 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 your table_cache value is large enough.
  •  optimizingThe server is performing initial optimizations for a query.
  •  preparingThis state occurs during query optimization.
  •  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 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 tableThe thread is removing an internal temporary table after processing a SELECT statement. 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 TABLE statement, has created the new table, and is renaming it to replace the original table.
  •  Reopen tablesThe 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.
  •  Repair by sortingThe repair code is using a sort to create indexes.
  •  Repair doneThe thread has completed a multi-threaded repair for a MyISAM table.
  •  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 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 updateThe 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 dataThe thread is reading and processing rows for a SELECT statement, and sending data to the client. Because operations occurring during this 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.
  •  setupThe thread is beginning an ALTER TABLE operation.
  •  Sorting for groupThe thread is doing a sort to satisfy a GROUP BY.
  •  Sorting for orderThe thread is doing a sort to satisfy a ORDER BY.
  •  Sorting indexThe thread is sorting index pages for more efficient access during a MyISAMtable optimization operation.
  •  Sorting resultFor a SELECT statement, this is similar to Creating sort index, but for nontemporary tables.
  •  statisticsThe 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.
  •  System lockThe 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. For SHOW PROFILE, this state means the thread is requesting the lock (not waiting for it).
  •  Table lockThe next thread state after System lock. The thread has acquired an external lock and is going to request an internal table lock.
  •  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 lockThe thread is going to request or is waiting for an advisory lock requested with a GET_LOCK() call. For SHOW PROFILE, this state means the thread is requesting the lock (not waiting for it).
  •  Waiting for release of readlockThe thread is waiting for a global read lock obtained by another thread (withFLUSH TABLES WITH READ LOCK) to be released.
  •  Waiting for tablesWaiting for tableThe 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 TABLEStbl_nameALTER TABLERENAME TABLEREPAIR TABLEANALYZE TABLE, or OPTIMIZE TABLE.

  •  Waiting on condA generic state in which the thread is waiting for a condition to become true. No specific state information is available.
  •  Waiting to get readlockThe thread has issued a FLUSH TABLES WITH READ LOCK statement to obtain a global read lock and is waiting to obtain the lock.
  •  Writing to netThe server is writing a packet to the network.

[root@ucjmh ~]# cat kill_lock.sql
kill 43;
kill 55;
[root@ucjmh ~]# cat locked.txt
43 root localhost hi_db Query 3825 Waiting for table metadata lock insert into t values(4)
55 root localhost NULL Query 0 NULL show processlist
[root@ucjmh ~]# cat ger_lock.sh
#!/bin/bash
rm -rf locked.txt
rm -rf kill_lock.sql
mysql -uroot -poracle -e “show processlist”|grep -i “Query” >> locked.txt;
for line in $(awk ‘{printf $1″\n”}’ locked.txt)
do
echo “kill ${line};”>>kill_lock.sql
done

MySQL隐式转换

mysql> create table users(num int not null, id varchar(30) not null, password varchar(30) not null, primary key(num));

Query OK, 0 rows affected (0.00 sec)

mysql> insert into users values(1, ‘admin’, ‘ad1234’);

Query OK, 1 row affected (0.00 sec)

mysql> insert into users values(2, ‘wh1ant’, ‘wh1234’);

Query OK, 1 row affected (0.00 sec)

mysql> insert into users values(3, ‘secuholic’, ‘se1234’);

Query OK, 1 row affected (0.00 sec)

mysql> select * from users where id=0;

+—–+———–+———-+

| num | id        | password |

+—–+———–+———-+

|   1 | admin     | ad1234   |

|   2 | wh1ant    | wh1234   |

|   3 | secuholic | se1234   |

+—–+———–+———-+

3 rows in set, 3 warnings (0.00 sec)

mysql> show warnings

-> ;

+———+——+———————————————–+

| Level   | Code | Message                                       |

+———+——+———————————————–+

| Warning | 1292 | Truncated incorrect DOUBLE value: ‘admin’     |

| Warning | 1292 | Truncated incorrect DOUBLE value: ‘wh1ant’    |

| Warning | 1292 | Truncated incorrect DOUBLE value: ‘secuholic’ |

+———+——+———————————————–+

3 rows in set (0.00 sec)

mysql> select * from users where id=’0′;

Empty set (0.00 sec)

mysql> select * from users where 0=id;

+—–+———–+———-+

| num | id        | password |

+—–+———–+———-+

|   1 | admin     | ad1234   |

|   2 | wh1ant    | wh1234   |

|   3 | secuholic | se1234   |

+—–+———–+———-+

3 rows in set, 3 warnings (0.00 sec)

mysql> insert into users values(‘ucjmh’,’ucjmh’,’ucjmh’);

Query OK, 1 row affected, 1 warning (0.00 sec)

mysql> show warnings;

+———+——+————————————————————+

| Level   | Code | Message                                                    |

+———+——+————————————————————+

| Warning | 1366 | Incorrect integer value: ‘ucjmh’ for column ‘num’ at row 1 |

+———+——+————————————————————+

1 row in set (0.00 sec)

mysql> select * from users;

+—–+———–+———-+

| num | id        | password |

+—–+———–+———-+

|   0 | ucjmh     | ucjmh    |

|   1 | admin     | ad1234   |

|   2 | wh1ant    | wh1234   |

|   3 | secuholic | se1234   |

+—–+———–+———-+

4 rows in set (0.00 sec)

如果是在oracle中直接会报ora-01722