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. jQuery Ajax(异步改同步)

    在实际使用中,我们经常会用的Ajax(异步加载,在不刷新整个网页的前提下对网页部分内容进行更新) 使用时,偶尔会遇上需要从一个接口中得到一个数组和数据对应的id,在另一个接口上再得到数据,最初写法如下 ...

  2. angular1结合webpack构建工具

    目录结构 webpack.config.js const { resolve } = require('path') const webpack = require('webpack') const ...

  3. arcgis C#判断点在线段的左右侧

    要判断一个点在一条线段的左侧还是右侧,从网上查到了这样一个算法.其实本质上就是一个sin角度的计算问题. 设线段端点为从A(x1,y1)到B(x2,y2),线外一点P(x0,y0),判断该点位于有向线 ...

  4. 【Linux】Linux远程登陆

    登录任务 Windows主机--远程登录--Linux主机 一.登陆前提准备 1.1 确保网络通畅 确保从Windows 能够Ping通Linux 1.2 关闭Linux防火墙 //前提:以root管 ...

  5. 怎样在vs2013和vs2015中实现自动编译sass

    Visual Studio不论是2013版本还是2015版本要自动编译都需要添加扩展. 添加扩展的方法,路径“工具”->“扩展和更新”,在打开的窗口“搜索”你需要的扩展根据提示“下载”和“安装” ...

  6. EPS 转 pdf 在线

    EPS 转 pdf 在线网站 https://convertio.co/zh/eps-pdf/

  7. matlab练习程序(生成黑白网格)

    提供了两种生成方法,一个是自己编程实现,比较灵活:另一个是调用系统的checkerboard函数,似乎只能生成8*8网格. 至于用途,也许可以用来下国际象棋. 自己函数生成: 系统函数生成: 代码如下 ...

  8. Appium 如何模拟返回按键

    from appium.webdriver import Remote driver.keyevent(4) python中点击返回键是这样写的 附录 keycode 电话键 KEYCODE_CALL ...

  9. ahp层次分析法软件

    http://www.jz5u.com/Soft/trade/Other/58808.html 权重计算 归一化 本组当前数 - 本组最小 / 本组最大-本组最小 http://blog.csdn.n ...

  10. (六)svn 服务器端使用之权限管理

    权限管理(了解) 认证授权机制 在企业开发中会为每位程序员.测试人员等相关人员分配一个账号,用户通过使用svn客户端连接svn服务时需要输入账号和密码,svn服务对账号和密码进行校验,输入正确可以继续 ...