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. IO流之递归

    递归: 递归,指在当前方法内调用自己的这种现象 public void method(){ System.out.println(“递归的演示”); //在当前方法内调用自己 method(); } ...

  2. Angular选项卡

    前几天我发的东西,可能对于没有基础的人很难理解,那么今天,咱们就发点简单点的东西吧! Angular显示隐藏,选项卡! 还是那句话,话不多说,上代码: <!DOCTYPE html> &l ...

  3. Eclipse中搭建Android开发环境

    一.搭建Android开发环境 准备工作:下载Eclipse.JDK.Android SDK.ADT插件 下载地址:Eclipse:http://www.eclipse.org/downloads/ ...

  4. ViewPager+fragment的使用

    如图我在一个继承FragmentActivity的类中嵌套了3个fragment分别能实现3个不同的界面,默认展现第一个,在第一个的fragment中有个ViewPager在ViewPager中嵌套了 ...

  5. 去掉iframe白色背景方法

    在iframe内添加如下代码 style="display:none" onload="this.style.display = 'block';" 先让它不显 ...

  6. scope 作用域(bean 的生存范围)

    默认是 singleton ,单例模式,如下代码: @Test public void testAddUser() throws Exception { ApplicationContext ctx ...

  7. C/S与B/S架构的区别和优缺点

    C/S 架构的概念 C/S是Client/Server,即客户端/服务器端架构,一种典型的两层架构. 客户端包含一个或多个在用户的电脑上运行的程序 服务器端有两种,一种是数据库服务器端,客户端通过数据 ...

  8. iBrand 开源电商小程序 (Laravel API+ webpack + gulp + 原生小程序)

    iBrand 社交电商产品正式进入开源过程中了,我们制定了详细的开源计划,目前已经发布了 V1 的版本,后续的版本也在陆续整理完善中. 各个版本功能明细如下图: 3 个版本计划在今年春节前全部完成,可 ...

  9. C# WinForm 程序免安装 .NET Framework(XP/win7/win10环境运行)

    前文 首先感谢群里的大神宇内流云 提供的anyexec for windows版本. 经过本人搭建虚拟机在xp环境 使用anyexec运行winfrom程序后,测试通过,如下是用的xp运行winfro ...

  10. vue cli 构建的 webpack 项目设置多页面

    1. webpack-dev-server下的设置(npm run dev) ./build/webpack.dev.conf.js 中,修改 new HtmlWebpackPlugin ,一个页面一 ...