有没有办法显示MYsql解释计划,如Oracle作为树

时间:2016-03-30 10:13:42

标签: mysql oracle explain

示例查询:

create table t1(c1 int primary key);

的MySQL

mysql> explain select c1 from t1;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra       |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
|  1 | SIMPLE      | t1    | NULL       | index | NULL          | PRIMARY | 4       | NULL |    1 |   100.00 | Using index |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set, 1 warning (0.00 sec)

的Oracle:

SQL> select * from table(dbms_xplan.display);

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 2822583898

------------------------------------------------------------------------------
| Id  | Operation         | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |          |     1 |    13 |     2   (0)| 00:00:01 |
|   1 |  TABLE ACCESS FULL| T_DILANG |     1 |    13 |     2   (0)| 00:00:01 |
------------------------------------------------------------------------------

Note
-----

PLAN_TABLE_OUTPUT
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
   - dynamic sampling used for this statement (level=2)

12 rows selected.

可以从tree解释计划中获取大量信息。如果MySQL能够显示它也会很棒。

更新

使用Percona Toolkit,pt-visual-explain,效果很好。

命令行输出:

mysql> explain select c1 from t1, A, B where c1 > 0 group by c1 having count(1) > 2 limit 1;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------------------------------------------+
| id | select_type | table | partitions | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra                                                     |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------------------------------------------+
|  1 | SIMPLE      | t1    | NULL       | index | PRIMARY       | PRIMARY | 4       | NULL |    1 |   100.00 | Using where; Using index; Using temporary; Using filesort |
|  1 | SIMPLE      | A     | NULL       | index | NULL          | idx7    | 4       | NULL |    1 |   100.00 | Using index; Using join buffer (Block Nested Loop)        |
|  1 | SIMPLE      | B     | NULL       | index | NULL          | idx7    | 4       | NULL |    1 |   100.00 | Using index; Using join buffer (Block Nested Loop)        |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+-----------------------------------------------------------+
3 rows in set, 1 warning (0.00 sec)

pt-visual-explain的输出:

$mysql -e "explain select c1 from t1, A, B where c1 > 0 group by c1 having count(1) > 2 limit 1" | pt-visual-explain
Filesort
+- TEMPORARY
   table          temporary(t1,A,B)
   +- JOIN
      +- Join buffer
      |  +- Index scan
      |     key            B->idx7
      |     key_len        4
      |     rows           1
      +- JOIN
         +- Join buffer
         |  +- Index scan
         |     key            A->idx7
         |     key_len        4
         |     rows           1
         +- Filter with WHERE
            +- Index scan
               key            t1->PRIMARY
               possible_keys  PRIMARY
               key_len        4
               rows           1

1 个答案:

答案 0 :(得分:2)

新的MySQL工作台附带Visual Explain Plan,它显示了解释查询的流程图。除了在命令行或mysql中,没有太多。

https://dev.mysql.com/doc/workbench/en/wb-performance-explain.html

如果你仍然喜欢命令行,你可以使用Percona的Visual Explain工具,但它需要解释计划作为给定的输入。

mysql -e“解释从t1选择c1”| PT-视觉解释

https://www.percona.com/doc/percona-toolkit/2.2/pt-visual-explain.html