了解EXPLAIN MYSQL命令的输出

时间:2017-06-19 11:51:20

标签: mysql sql

我在QOVENDOR表上运行此命令。

EXPLAIN SELECT * 
FROM QOVENDOR
WHERE V_NAME LIKE "B%"
ORDER BY V_AREACODE

QOVENDOR表:

CREATE TABLE `qovendor` (
  `V_CODE` int(11) NOT NULL,
  `V_NAME` varchar(35) NOT NULL,
  `V_CONTACT` varchar(15) NOT NULL,
  `V_AREACODE` char(3) NOT NULL,
  `V_PHONE` char(8) NOT NULL,
  `V_STATE` char(2) NOT NULL,
  `V_ORDER` char(1) NOT NULL,
  PRIMARY KEY (`V_CODE`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1

我得到的输出是:

+------+-------------+----------+------+---------------+------+---------+------+------+-----------------------------+
| id   | select_type | table    | type | possible_keys | key  | key_len | ref  | rows | Extra                       |
+------+-------------+----------+------+---------------+------+---------+------+------+-----------------------------+
|    1 | SIMPLE      | QOVENDOR | ALL  | NULL          | NULL | NULL    | NULL |   15 | Using where; Using filesort |
+------+-------------+----------+------+---------------+------+---------+------+------+-----------------------------+

这些信息可以帮助我构建更有效的查询,但我很难理解如何。唯一具有一些间接信息的非空列是Extra,select_ype,Type和Rows。我确实在使用where子句,不确定Using filesort是什么意思,除了它与order by有关。如何判断此查询是否最有效?

为了评估性能,我应该有一些CPU成本和时间数据(比如Oracle DBMS提供的EXPLAIN命令)。

1 个答案:

答案 0 :(得分:0)

在MySQL中,explain不会为您提供时间,也不会为您提供CPU成本信息。 MySQL documentation on explain准确描述了输出中每列的含义:

Column         JSON Name        Meaning
id             select_id        The SELECT identifier
select_type    None             The SELECT type
table          table_name       The table for the output row
partitions     partitions       The matching partitions
type           access_type      The join type
possible_keys  possible_keys    The possible indexes to choose
key            key              The index actually chosen
key_len        key_length       The length of the chosen key
ref            ref              The columns compared to the index
rows           rows             Estimate of rows to be examined
filtered       filtered         Percentage of rows filtered by table condition
Extra          None             Additional information

从查询优化的角度来看,类型,possible_keys,键,行和额外字段可能是最重要的。他们的详细描述可以在链接的MySQL文档中找到。