Preface
 
    As usual we will check the MySQL executed plan of SQL query by execute "explain select ... ;".It's a simple way to get the information of executed plan.Furthermore,we can also get a json format execution plan by execute "explain format=json select ... ;" for more detail of SQL query.Alternatively,we can also get another kind of execution plan organized by a tree modality.Well,what is that then?
 
Introduce
 
    pt-visual-explain relies on MySQL explain.It provides a easy-to-understand way by truning original explain output into a tree modaity.The tree is left-deep and depth-first(see it from bottom to roof).Its parameters are very simple(almost least in most of the tools in Percona-Toolkit).Let's see the details.
 
Procedure
 
Usage
 pt-visual-explain [OPTIONS] [FILES]
Parameter
 --clustered-pk -- For innodb,it allows primary key index access not to use bookmark lookup.
--format -- Set the type of output(default "tree",others "dump").
--connect -- Specify a followed file which contains a query and output result of explain on the query.
--database -- Specify which database to connect.
--host -- Specify connection hostname.
--port -- Specify connection port.
--user -- Specify connection user.
--password -- Specify connection password.
--socket -- Specify connection socket.
Examples
 
Create test table and insert rows into them(you can use procedure to do this).
 root@localhost:mysql3306.sock [zlm]>show create table customer\G
*************************** . row ***************************
Table: customer
Create Table: CREATE TABLE `customer` (
`id` int() unsigned NOT NULL AUTO_INCREMENT,
`order_id` int() unsigned NOT NULL DEFAULT '',
`name` varchar() NOT NULL DEFAULT '',
`gender` enum('male','female') NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf8mb4
row in set (0.00 sec) root@localhost:mysql3306.sock [zlm]>show create table goods\G
*************************** . row ***************************
Table: goods
Create Table: CREATE TABLE `goods` (
`id` int() unsigned NOT NULL AUTO_INCREMENT,
`order_id` int() unsigned NOT NULL,
`goodsname` varchar() NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT= DEFAULT CHARSET=utf8mb4
row in set (0.00 sec)
Generate a tree using a file which contains a query statement.
 [root@zlm1 :: ~]
#echo "select count(*) from customer join goods using(order_id);" > query1.sql [root@zlm1 :: ~]
#pt-visual-explain -h192.168.56. -P3306 -urepl -prepl4slave -Dzlm --connect query1.sql
JOIN
+- Join buffer
| +- Filter with WHERE
| +- Table scan -- It means "customer" is a drived table,do full table scan.
| rows
| +- Table
| table customer
+- Table scan -- It means "goods" is a drive table,do full table scan,too.
rows
+- Table
table goods [root@zlm1 :: ~]
#
Compare the original explain result with the output above.
 root@localhost:mysql3306.sock [zlm]>explain select count(*) from customer join goods using(order_id);
+----+-------------+----------+------------+------+---------------+------+---------+------+-------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+------+---------------+------+---------+------+-------+----------+----------------------------------------------------+
| | SIMPLE | goods | NULL | ALL | NULL | NULL | NULL | NULL | | 100.00 | NULL |
| | SIMPLE | customer | NULL | ALL | NULL | NULL | NULL | NULL | | 10.00 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+----------+------------+------+---------------+------+---------+------+-------+----------+----------------------------------------------------+
rows in set, warning (0.00 sec) ###The output of explain is compatiable with the output of tree above.###
Generate a tree using a file which contains a explain output.
 [root@zlm1 :: ~]
#mysql -e "use zlm;explain select count(*) from customer join goods where goods.goodsname='cellphone';" > explain1.log [root@zlm1 :: ~]
#pt-visual-explain -h192.168.56. -P3306 -urepl -prepl4slave explain1.log
JOIN
+- Join buffer
| +- Index scan -- It means "customer" is a drive table,do index scan with primary.
| key customer->PRIMARY
| key_len
| rows
+- Filter with WHERE
+- Table scan -- It means "goods" is a drive table,do full table scan,too.
rows
+- Table
table goods [root@zlm1 :: ~]
#
Compare the original explain result with the output above.
 root@localhost:mysql3306.sock [zlm]>explain select count(*) from customer join goods where goods.goodsname='cellphone';
+----+-------------+----------+------------+-------+---------------+---------+---------+------+-------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+-------+---------------+---------+---------+------+-------+----------+----------------------------------------------------+
| | SIMPLE | goods | NULL | ALL | NULL | NULL | NULL | NULL | | 10.00 | Using where |
| | SIMPLE | customer | NULL | index | NULL | PRIMARY | | NULL | | 100.00 | Using index; Using join buffer (Block Nested Loop) |
+----+-------------+----------+------------+-------+---------------+---------+---------+------+-------+----------+----------------------------------------------------+
rows in set, warning (0.00 sec)
Generate a tree using standard input of MySQL command line with "-e" parameter.
 [root@zlm1 :: ~]
#mysql -e "use zlm;explain select c.name,c.gender,g.goodsname from goods g,customer c where c.order_id=g.order_id and c.id<=5;" | pt-visual-explain
JOIN
+- Join buffer
| +- Filter with WHERE
| +- Table scan
| rows
| +- Table
| table g -- Show table with alias "g" and it's a dirved table,do full table scan.
+- Filter with WHERE
+- Bookmark lookup -- If you're using only innodb table,this kind of lookup will lead to bad performance.
+- Table
| table c -- Show table with alias "c" and it's a drive table,do index range scan.
| possible_keys PRIMARY
+- Index range scan
key c->PRIMARY
possible_keys PRIMARY
key_len
rows [root@zlm1 :: ~]
#select c.name,c.gender,g.goodsname from goods g,customer c where c.order_id=g.order_id and c.id<=;
+------+--------+-----------+
| name | gender | goodsname |
+------+--------+-----------+
| zlm | male | tv |
| zlm | male | tv |
| zlm | male | tv |
| zlm | male | tv |
| zlm | male | tv |
| zlm | male | cd |
| zlm | male | cd |
| zlm | male | cd |
| zlm | male | cd |
| zlm | male | cd |
| zlm | male | dvd |
| zlm | male | dvd |
| zlm | male | dvd |
| zlm | male | dvd |
| zlm | male | dvd |
| zlm | male | cellphone |
| zlm | male | cellphone |
| zlm | male | cellphone |
| zlm | male | cellphone |
| zlm | male | cellphone |
| zlm | male | computer |
| zlm | male | computer |
| zlm | male | computer |
| zlm | male | computer |
| zlm | male | computer |
+------+--------+-----------+
rows in set (0.00 sec)
Compare the original explain result with the output above.
 root@localhost:mysql3306.sock [zlm]>explain select c.name,c.gender,g.goodsname from goods g,customer c where c.order_id=g.order_id and c.id<=;
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+----------------------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+----------------------------------------------------+
| | SIMPLE | c | NULL | range | PRIMARY | PRIMARY | | NULL | | 100.00 | Using where |
| | SIMPLE | g | NULL | ALL | NULL | NULL | NULL | NULL | | 10.00 | Using where; Using join buffer (Block Nested Loop) |
+----+-------------+-------+------------+-------+---------------+---------+---------+------+------+----------+----------------------------------------------------+
rows in set, warning (0.00 sec)
As the test tables are both innodb tables,use “--clustered-pk" option is recommended.
 
 [root@zlm1 :: ~]
#mysql -e "use zlm;explain select c.name,c.gender,g.goodsname from goods g,customer c where c.order_id=g.order_id and c.id<=5;" | pt-visual-explain --clustered-pk
JOIN
+- Join buffer
| +- Filter with WHERE
| +- Table scan
| rows
| +- Table
| table g
+- Filter with WHERE
+- Index range scan -- This time the "bookmark lookup" is missing.It will lookup by pk directly what is more efficient way.
key c->PRIMARY
possible_keys PRIMARY
key_len
rows
Summary
  • The "--clustered-pk" is only for innodb case to avoid bookmark lookup.
  • If you specify the "--connect" option, a file contains SQL query need to be used,too.
  • pt-visual-explain depends on explain of MySQL and provides several ways to generate trees.
  • The information of pt-visual-explain is limited,if you want to get more details such as "cost_info","query_cost",etc.You'd better use json format of original MySQL explain.
 

Percona-Tookit工具包之pt-visual-explain的更多相关文章

  1. [转]细说MySQL Explain和Optimizer Trace简介

    在开发过程中,对每个上线的SQL查询指纹(query figerprint)的质量都应有估算:而估算DB查询质量最直接的方法,就是分析其查询执行计划( Query Execution Plan ,即Q ...

  2. 016:Explain

    一. Explain EXPLAIN 官方文档 1.explain说明 explain是解释SQL语句的执行计划,即显示该SQL语句怎么执行的 使用explain的时候,也可以使用desc 5.6 版 ...

  3. 索引及explain

    索引好比书的目录.通过索引能快速的定位到一条数据. 在MySQL中除了B+树索引之外,还有一些其他的索引类型.比如:全文索引.(DB和DD索引叫R树索引).在MySQL cluster中是P树索引,m ...

  4. Linux后台开发工具箱

    https://files-cdn.cnblogs.com/files/aquester/Linux后台开发工具箱.pdf 目录 目录 1 1. 前言 3 2. 脚本类工具 3 2.1. sed命令- ...

  5. Mysql: pt-table-checksum 和 pt-table-sync 检查主从一致性,实验过程

    一.安装 percona 包 1.安装仓库的包 https://www.percona.com/doc/percona-repo-config/yum-repo.html sudo yum insta ...

  6. Linux后台开发工具箱-葵花宝典

    Linux后台开发工具箱-葵花宝典 一见 2016/11/4 目录 目录 1 1. 前言 4 2. 脚本类工具 4 2.1. 双引号和单引号 4 2.2. 取脚本完整文件路径 5 2.3. 环境变量和 ...

  7. [知识库分享系列] 二、.NET(ASP.NET)

    最近时间又有了新的想法,当我用新的眼光在整理一些很老的知识库时,发现很多东西都已经过时,或者是很基础很零碎的知识点.如果分享出去大家不看倒好,更担心的是会误人子弟,但为了保证此系列的完整,还是选择分享 ...

  8. 使用Apache Spark 对 mysql 调优 查询速度提升10倍以上

    在这篇文章中我们将讨论如何利用 Apache Spark 来提升 MySQL 的查询性能. 介绍 在我的前一篇文章Apache Spark with MySQL 中介绍了如何利用 Apache Spa ...

  9. 推荐几款MySQL相关工具

    前言: 随着互联网技术的不断发展, MySQL 相关生态也越来越完善,越来越多的工具涌现出来.一些公司或个人纷纷开源出一些不错的工具,本篇文章主要介绍几款 MySQL 相关实用工具.提醒下,这里并不介 ...

  10. db2基础

    DB2知识文档 一.db2 基础 基本语法 注释:"--"(两个减号) 字符串连接:"||" 如set msg='aaaa'||'bbbb',则msg为'aaa ...

随机推荐

  1. Spring课程 Spring入门篇 5-3 配置切入点 pointcut

    1 解析 1.1 xml常见的配置切入点写法 2 代码演练 2.1 xml配置切入点   1 解析 1.1 xml常见的配置切入点写法 2 代码演练 2.1 xml配置切入点 xml配置: <? ...

  2. SVN服务器在Ubuntu16.04下搭建多版本库详细教程

    1  介绍  Subversion是一个自由,开源的版本控制系统,这个版本库就像一个普通的文件服务器,不同的是,它可以记录每一次文件和目录的修改情况.这样就可 以很方面恢复到以前的版本,并可以查看数据 ...

  3. CSS3 常用新特性总结

    更新于(2017.07.07)会总结项目中比较常用的有些CSS属性 伪类选择器 E:first-of-type: 匹配同类型中的第一个同级兄弟元素E E:last-of-type: 匹配同类型中的最后 ...

  4. C#工具类之数据库连接

    一.SQL Server /// <summary> /// 数据库的通用访问代码 /// 此类为抽象类, /// 不允许实例化,在应用时直接调用即可 /// </summary&g ...

  5. STROME --realtime & online parallel computing

    Data Collections ---> Stream to Channel (as source input) ----> Parallel Computing---> Resu ...

  6. 【Linux】GDB程序调试

    一.GDB简介 GDB是GNU发布的一款功能强大的程序调试工具.GDB主要完成下面三个方面的功能: 启动被调试程序. 让被调试的程序在指定的位置停住. 当程序被停住时,可以检查程序状态(如变量值) 二 ...

  7. head标签必不可少的元素

    <head> 标签用于定义文档的头部,它是所有头部元素的容器.<head> 中的元素可以引用脚本.指示浏览器在哪里找到样式表.提供元信息等等. 文档的头部描述了文档的各种属性和 ...

  8. HTTP协议安全头部X-Content-Type-Options引入的问题

    前段时间测试MM反馈了一个问题,在富文本编辑器里上传的图片无法正常呈现.因为Jackie在本机的环境上没有观察类似的现象,而恰好那天测试环境的某个重要配项被改错了,于是Jackie想当然的归类为配置项 ...

  9. Android端访问服务器核心代码

  10. NodeJs安装less(npm方式)

    上一次讲了如何在浏览器端解析less文件,这次是在cmd中使用npm中的less模块来解析 详解如下 首下我们去下载一个https://nodejs.org/en/,   一路next之后,因为文件不 ...