首页 > 数据库 >mysql使用desc(describle)和explain查看执行计划--笔记

mysql使用desc(describle)和explain查看执行计划--笔记

时间:2022-11-15 17:59:20浏览次数:38  
标签:-- tdb explain dba mon mysql hlf NULL tbl

大家查看mysql执行计划时用的最多的是explain,其实还可以等效使用desc、describle查看执行计划,desc和explain命令还可以有别的作用如查看表列属性等功能。

1、查看表结构和相关信息

dba_mon@tdb_hlf>show create table tbl_read\G
*************************** 1. row ***************************
       Table: tbl_read
Create Table: CREATE TABLE `tbl_read` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8mb4
1 row in set (0.00 sec)

dba_mon@tdb_hlf>select * from tbl_read;
+----+----------+
| id | name     |
+----+----------+
|  1 | hlf      |
|  3 | NULL     |
|  4 | zhangsan |
|  5 | lisi     |
|  6 | mazi     |
+----+----------+
5 rows in set (0.00 sec)

dba_mon@tdb_hlf>

dba_mon@tdb_hlf>show index from tbl_read;
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| Table    | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
| tbl_read |          0 | PRIMARY  |            1 | id          | A         |           5 |     NULL | NULL   |      | BTREE      |         |               |
+----------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+
1 row in set (0.00 sec)


dba_mon@tdb_hlf>show columns from tbl_read;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(50)      | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

dba_mon@tdb_hlf>

dba_mon@tdb_hlf>show table status like 'tbl_read'\G
*************************** 1. row ***************************
           Name: tbl_read
         Engine: InnoDB
        Version: 10
     Row_format: Compact
           Rows: 5
 Avg_row_length: 3276
    Data_length: 16384
Max_data_length: 0
   Index_length: 0
      Data_free: 0
 Auto_increment: 7
    Create_time: 2020-06-29 19:21:08
    Update_time: NULL
     Check_time: NULL
      Collation: utf8mb4_general_ci
       Checksum: NULL
 Create_options: 
        Comment: 
1 row in set (0.00 sec)

dba_mon@tdb_hlf>

2、使用desc(describle)查看执行计划
dba_mon@tdb_hlf>help desc;
Many help items for your request exist.
To make a more specific request, please type 'help <item>',
where <item> is one of the following
topics:
   EXPLAIN
   GROUP_CONCAT
   SELECT

dba_mon@tdb_hlf>desc tbl_read;             
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(50)      | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

dba_mon@tdb_hlf>

dba_mon@tdb_hlf>desc tbl_read name;       
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

dba_mon@tdb_hlf>desc tbl_read id;  
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
+-------+------------------+------+-----+---------+----------------+
1 row in set (0.00 sec)

dba_mon@tdb_hlf>

dba_mon@tdb_hlf>desc select * from tbl_read order by rand();                  
+----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+
| id | select_type | table    | type | possible_keys | key  | key_len | ref  | rows | Extra                           |
+----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+
|  1 | SIMPLE      | tbl_read | ALL  | NULL          | NULL | NULL    | NULL |    5 | Using temporary; Using filesort |
+----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+
1 row in set (0.00 sec)

dba_mon@tdb_hlf>
dba_mon@tdb_hlf>desc extended select * from tbl_read order by rand();
+----+-------------+----------+------+---------------+------+---------+------+------+----------+---------------------------------+
| id | select_type | table    | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra                           |
+----+-------------+----------+------+---------------+------+---------+------+------+----------+---------------------------------+
|  1 | SIMPLE      | tbl_read | ALL  | NULL          | NULL | NULL    | NULL |    5 |   100.00 | Using temporary; Using filesort |
+----+-------------+----------+------+---------------+------+---------+------+------+----------+---------------------------------+
1 row in set, 1 warning (0.01 sec)

dba_mon@tdb_hlf>show warnings;
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------+
| Level | Code | Message                                                                                                                                 |
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------+
| Note  | 1003 | /* select#1 */ select `tdb_hlf`.`tbl_read`.`id` AS `id`,`tdb_hlf`.`tbl_read`.`name` AS `name` from `tdb_hlf`.`tbl_read` order by rand() |
+-------+------+-----------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

dba_mon@tdb_hlf>desc extended select * from tbl_read order by name;          
+----+-------------+----------+------+---------------+------+---------+------+------+----------+----------------+
| id | select_type | table    | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra          |
+----+-------------+----------+------+---------------+------+---------+------+------+----------+----------------+
|  1 | SIMPLE      | tbl_read | ALL  | NULL          | NULL | NULL    | NULL |    5 |   100.00 | Using filesort |
+----+-------------+----------+------+---------------+------+---------+------+------+----------+----------------+
1 row in set, 1 warning (0.00 sec)

dba_mon@tdb_hlf>
dba_mon@tdb_hlf>
dba_mon@tdb_hlf>desc extended select * from tbl_read order by id;  
+----+-------------+----------+-------+---------------+---------+---------+------+------+----------+-------+
| id | select_type | table    | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra |
+----+-------------+----------+-------+---------------+---------+---------+------+------+----------+-------+
|  1 | SIMPLE      | tbl_read | index | NULL          | PRIMARY | 4       | NULL |    5 |   100.00 | NULL  |
+----+-------------+----------+-------+---------------+---------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.01 sec)

dba_mon@tdb_hlf>

 

dba_mon@tdb_hlf>desc partitions select * from tbl_read order by rand();
+----+-------------+----------+------------+------+---------------+------+---------+------+------+---------------------------------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra                           |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+---------------------------------+
|  1 | SIMPLE      | tbl_read | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    5 | Using temporary; Using filesort |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+---------------------------------+
1 row in set (0.01 sec)

dba_mon@tdb_hlf>
dba_mon@tdb_hlf>
dba_mon@tdb_hlf>desc partitions select * from tbl_read order by name;  
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra          |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------------+
|  1 | SIMPLE      | tbl_read | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    5 | Using filesort |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+----------------+
1 row in set (0.00 sec)

dba_mon@tdb_hlf>
dba_mon@tdb_hlf>
dba_mon@tdb_hlf>desc partitions select * from tbl_read order by id;  
+----+-------------+----------+------------+-------+---------------+---------+---------+------+------+-------+
| id | select_type | table    | partitions | type  | possible_keys | key     | key_len | ref  | rows | Extra |
+----+-------------+----------+------------+-------+---------------+---------+---------+------+------+-------+
|  1 | SIMPLE      | tbl_read | NULL       | index | NULL          | PRIMARY | 4       | NULL |    5 | NULL  |
+----+-------------+----------+------------+-------+---------------+---------+---------+------+------+-------+
1 row in set (0.00 sec)

dba_mon@tdb_hlf>

 

dba_mon@tdb_hlf>describe tbl_read;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(50)      | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

dba_mon@tdb_hlf>

 

dba_mon@tdb_hlf>describe extended select * from tbl_read order by id; 
+----+-------------+----------+-------+---------------+---------+---------+------+------+----------+-------+
| id | select_type | table    | type  | possible_keys | key     | key_len | ref  | rows | filtered | Extra |
+----+-------------+----------+-------+---------------+---------+---------+------+------+----------+-------+
|  1 | SIMPLE      | tbl_read | index | NULL          | PRIMARY | 4       | NULL |    5 |   100.00 | NULL  |
+----+-------------+----------+-------+---------------+---------+---------+------+------+----------+-------+
1 row in set, 1 warning (0.00 sec)

dba_mon@tdb_hlf>

3、使用explain查看执行计划

dba_mon@tdb_hlf>help explain;
Name: 'EXPLAIN'
Description:
Syntax:
{EXPLAIN | DESCRIBE | DESC}
    tbl_name [col_name | wild]

{EXPLAIN | DESCRIBE | DESC}
    [explain_type]
    explainable_stmt

explain_type: {
    EXTENDED
  | PARTITIONS
  | FORMAT = format_name
}

format_name: {
    TRADITIONAL
  | JSON
}

explainable_stmt: {
    SELECT statement
  | DELETE statement
  | INSERT statement
  | REPLACE statement
  | UPDATE statement
}

The DESCRIBE and EXPLAIN statements are synonyms. In practice, the
DESCRIBE keyword is more often used to obtain information about table
structure, whereas EXPLAIN is used to obtain a query execution plan
(that is, an explanation of how MySQL would execute a query).

URL: http://dev.mysql.com/doc/refman/5.6/en/explain.html

dba_mon@tdb_hlf>explain tbl_read;  
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
| name  | varchar(50)      | YES  |     | NULL    |                |
+-------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

dba_mon@tdb_hlf>
dba_mon@tdb_hlf>
dba_mon@tdb_hlf>explain tbl_read id;
+-------+------------------+------+-----+---------+----------------+
| Field | Type             | Null | Key | Default | Extra          |
+-------+------------------+------+-----+---------+----------------+
| id    | int(11) unsigned | NO   | PRI | NULL    | auto_increment |
+-------+------------------+------+-----+---------+----------------+
1 row in set (0.00 sec)

dba_mon@tdb_hlf>
dba_mon@tdb_hlf>explain tbl_read name;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| name  | varchar(50) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
1 row in set (0.00 sec)

dba_mon@tdb_hlf>

dba_mon@tdb_hlf>explain select * from tbl_read order by rand();
+----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+
| id | select_type | table    | type | possible_keys | key  | key_len | ref  | rows | Extra                           |
+----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+
|  1 | SIMPLE      | tbl_read | ALL  | NULL          | NULL | NULL    | NULL |    5 | Using temporary; Using filesort |
+----+-------------+----------+------+---------------+------+---------+------+------+---------------------------------+
1 row in set (0.00 sec)

dba_mon@tdb_hlf>
dba_mon@tdb_hlf>explain extended select * from tbl_read order by rand();        
+----+-------------+----------+------+---------------+------+---------+------+------+----------+---------------------------------+
| id | select_type | table    | type | possible_keys | key  | key_len | ref  | rows | filtered | Extra                           |
+----+-------------+----------+------+---------------+------+---------+------+------+----------+---------------------------------+
|  1 | SIMPLE      | tbl_read | ALL  | NULL          | NULL | NULL    | NULL |    5 |   100.00 | Using temporary; Using filesort |
+----+-------------+----------+------+---------------+------+---------+------+------+----------+---------------------------------+
1 row in set, 1 warning (0.00 sec)

dba_mon@tdb_hlf>
dba_mon@tdb_hlf>explain partitions select * from tbl_read order by rand();
+----+-------------+----------+------------+------+---------------+------+---------+------+------+---------------------------------+
| id | select_type | table    | partitions | type | possible_keys | key  | key_len | ref  | rows | Extra                           |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+---------------------------------+
|  1 | SIMPLE      | tbl_read | NULL       | ALL  | NULL          | NULL | NULL    | NULL |    5 | Using temporary; Using filesort |
+----+-------------+----------+------------+------+---------------+------+---------+------+------+---------------------------------+
1 row in set (0.01 sec)

dba_mon@tdb_hlf>

标签:--,tdb,explain,dba,mon,mysql,hlf,NULL,tbl
From: https://www.cnblogs.com/hsjz-xinyuan/p/16893293.html

相关文章

  • 延时求和波束形成的MATLAB仿真
    仿真结果如下:  核心代码如下:%------延时求和波束形成-------closeall;clearall;clc;%%%%%%基阵的有关参数c=1500;%m/sL=1.3;......
  • 删除文件中的某一行
    这种情况只能先把该文件全部读取下来,比如说读成一个String字符串,然后用String的方法处理然后把这些数据写回文件,和原来文件同名,就相当于覆盖该文件其实从windows的记事本......
  • 不用app实现shopify跳转亚马逊
    shopify跳转亚马逊Amazon的方式有很多种,如果不用app实现shopifyredirecttoAmazon呢?安装app一般都会带有附加文件从而降低网站加载速度,而速度是用户体验/转化率很重要的......
  • 便利店APP/小程序开发
    近年来,线下便利店也在努力寻求转型升级的机会,而APP/小程序开发就是更好的选择,商家可以在线销售便利店的产品。便利店APP/小程序开发都具备哪些功能?1、商品展示功能:......
  • xxl-job 容器化部署时实现自动注册
    集群环境机器A:部署xxl-job-admin机器B:部署xxl-job-executor机器A的物理网卡ip地址为x.x.x.x,机器B的物理网卡ip地址为y.y.y.y准备修改执行器yml......
  • MarkDown教程
    MarkDown基础基础篇视频讲解链接画图篇视频讲解链接标题#标题名字(井号的个数代表标题的级数)一级标题使用1个#二级标题使用2个#三级标题使用3个#四级标题使4用个#......
  • ICEM薄壁特征
    ......
  • Linux CentOS各版本修改yum源报错及解决
    报错1:执行 wget-O/etc/yum.repos.d/CentOS-Base.repohttps://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo   报错2:执行 wget-O/etc/yum.repos.d/Cen......
  • Java Instrumentation
    前言JDK1.5开始,Java新增了Instrumentation(JavaAgentAPI)和JVMTI(JVMToolInterface)功能,允许JVM在加载某个class文件之前对其字节码进行修改,同时也支持对......
  • APICloud APP开发指南
    移动端开发大致分为以下四个级别,下面分别介绍了每个级别需要了解和掌握的内容:初级熟悉HTML/CSS/JS前端技术,了解APICloud开发,能使用开发工具运行和预览模板项目,能使......