Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
3
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

@mapk0y

pt-visual-explain

MySQL の EXPLAIN を見やすくしてくれるツール

お手軽に使ってみる

準備

$ wget http://percona.com/get/pt-visual-explain
$ chmod +x ./pt-visual-explain

テストで使うテーブル

$ # new_table
$ mysql -h ${MYSQL_HOST} -uadmin sample -e 'select * from new_table'
+----+------+-----+
| id | name | age |
+----+------+-----+
|  1 | foo  |   1 |
|  2 | bar  |   2 |
|  3 | bazz |   3 |
+----+------+-----+
$ # new_table2
$ mysql -h ${MYSQL_HOST} -uadmin sample -e 'select * from new_table2'
+------+--------+
| name | profit |
+------+--------+
| bar  |   5000 |
| bazz |   3200 |
| foo  |  10000 |
+------+--------+

普通の EXPALIN

$ mysql -h ${MYSQL_HOST} -uadmin sample \
-e 'explain select new_table.name, new_table.age, new_table2.profit from new_table left join new_table2 on new_table.name = new_table2.name'
+----+-------------+------------+--------+---------------+---------+---------+-----------------------+------+-------+
| id | select_type | table      | type   | possible_keys | key     | key_len | ref                   | rows | Extra |
+----+-------------+------------+--------+---------------+---------+---------+-----------------------+------+-------+
|  1 | SIMPLE      | new_table  | ALL    | NULL          | NULL    | NULL    | NULL                  |    3 |       |
|  1 | SIMPLE      | new_table2 | eq_ref | PRIMARY       | PRIMARY | 182     | sample.new_table.name |    1 |       |
+----+-------------+------------+--------+---------------+---------+---------+-----------------------+------+-------+

EXPAIN の結果を pt-visual-explain に食わせた場合

$ mysql -h ${MYSQL_HOST} -uadmin sample \
-e 'explain select new_table.name, new_table.age, new_table2.profit from new_table left join new_table2 on new_table.name = new_table2.name' \
| ./pt-visual-explain 

JOIN
+- Bookmark lookup
|  +- Table
|  |  table          new_table2
|  |  possible_keys  PRIMARY
|  +- Unique index lookup
|     key            new_table2->PRIMARY
|     possible_keys  PRIMARY
|     key_len        182
|     ref            sample.new_table.name
|     rows           1
+- Table scan
   rows           3
   +- Table
      table          new_table
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
3
Help us understand the problem. What are the problem?