开发指南—DAL语句—EXPLAIN

本文涉及的产品
云原生数据库 PolarDB 分布式版,标准版 2核8GB
简介: 该语句用于解释SQL语句的执行计划,包括SELECT、DELETE、INSERT、REPLACE或UPDATE语句。

语法

获取SQL计划信息:


EXPLAIN
{LOGICALVIEW | LOGIC | SIMPLE | DETAIL | EXECUTE | PHYSICAL | OPTIMIZER | SHARDING
 | COST | ANALYZE | BASELINE | JSON_PLAN | ADVISOR} 
 {SELECT statement | DELETE statement | INSERT statement | REPLACE statement| UPDATE statement}

示例

  • explain语句:展示基本的SQL执行计划,该执行计划是算子组成,主要体现SQL在CN上的整个执行过程。
mysql> explain select count(*) from lineitem group by L_ORDERKEY;
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| LOGICAL EXECUTIONPLAN                                                                                                                                                              |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Project(count(*)="count(*)")                                                                                                                                                       |
|   HashAgg(group="L_ORDERKEY", count(*)="SUM(count(*))")                                                                                                                            |
|     Gather(concurrent=true)                                                                                                                                                        |
|       LogicalView(tables="[000000-000003].lineitem_[00-15]", shardCount=16, sql="SELECT `L_ORDERKEY`, COUNT(*) AS `count(*)` FROM `lineitem` AS `lineitem` GROUP BY `L_ORDERKEY`") |
| HitCache:false                                                                                                                                                                     |                                                                                                                                                               |
| TemplateId: 5819c807                                                                                                                                                               |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  • 其中,HitCache标记该查询是否命中PlanCache,取值为false or trueTemplateId表示对该计划的标识,具有全局唯一性。
  • explain logicalview语句:展示LogicalView所表示的下推SQL在DN上的执行计划。
mysql> explain LOGICALVIEW select  mysql> explain select logialview count(*) from lineitem group by L_ORDERKEY;
+----------------------------------------------------------+
| LOGICAL EXECUTIONPLAN                                    |
+----------------------------------------------------------+
| Project(count(*)="count(*)")                             |
|   HashAgg(group="L_ORDERKEY", count(*)="SUM(count(*))")  |
|     Gather(concurrent=true)                              |
|       LogicalView                                        |
|         MysqlAgg(group="L_ORDERKEY", count(*)="COUNT()") |
|           MysqlTableScan(name=[ads, lineitem])           |
| HitCache:true                                            |
| Source:PLAN_CACHE                                        |
| TemplateId: 5819c807
  • explain execute语句:表示下推SQL在mysql的执行情况,这个语句和mysql的explain语句同义。通过该语句可以查看下推SQL在DN上有没有使用索引,有没有做全表扫描。
mysql> explain EXECUTE  select  count(*) from lineitem group by L_ORDERKEY;
+----+-------------+----------+------------+-------+---------------+---------+---------+-----+------+----------+----------------------------------------------+
| id | select_type | table    | partitions | type  | possible_keys | key     | key_len | ref | rows | filtered | Extra                                        |
+----+-------------+----------+------------+-------+---------------+---------+---------+-----+------+----------+----------------------------------------------+
| 1  | SIMPLE      | lineitem | NULL       | index | PRIMARY       | PRIMARY | 8       | NULL | 1    | 100      | Using index; Using temporary; Using filesort |
+----+-------------+----------+------------+-------+---------------+---------+---------+-----+------+----------+----------------------------------------------+
1 row in set (0.24 sec)
  • explain sharding语句:展示当前查询在DN上扫描的物理分片情况。
mysql> explain sharding  select  count(*) from lineitem group by L_ORDERKEY;
+---------------+----------------------------------+-------------+-----------+-----------+
| LOGICAL_TABLE | SHARDING                         | SHARD_COUNT | BROADCAST | CONDITION |
+---------------+----------------------------------+-------------+-----------+-----------+
| lineitem      | [000000-000003].lineitem_[00-15] | 16          | false     |           |
+---------------+----------------------------------+-------------+-----------+-----------+
1 row in set (0.04 sec)
  • explain cost语句:相对于explain语句,除了展示执行计划以外,还会显示各个算子基于统计信息估算的代价,以及这条查询被优化器识别的WORKLOAD。
mysql> explain COST  select  count(*) from lineitem group by L_ORDERKEY;
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| LOGICAL EXECUTIONPLAN                                                                                                                                                                                                                                                                                            |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Project(count(*)="count(*)"): rowcount = 2508.0, cumulative cost = value = 2.4867663E7, cpu = 112574.0, memory = 88984.0, io = 201.0, net = 4.75, id = 182                                                                                                                                                       |
|   HashAgg(group="L_ORDERKEY", count(*)="SUM(count(*))"): rowcount = 2508.0, cumulative cost = value = 2.4867662E7, cpu = 112573.0, memory = 88984.0, io = 201.0, net = 4.75, id = 180                                                                                                                            |
|     Gather(concurrent=true): rowcount = 2508.0, cumulative cost = value = 2.4860069E7, cpu = 105039.0, memory = 29796.0, io = 201.0, net = 4.75, id = 178                                                                                                                                                        |
|       LogicalView(tables="[000000-000003].lineitem_[00-15]", shardCount=16, sql="SELECT `L_ORDERKEY`, COUNT(*) AS `count(*)` FROM `lineitem` AS `lineitem` GROUP BY `L_ORDERKEY`"): rowcount = 2508.0, cumulative cost = value = 2.4860068E7, cpu = 105038.0, memory = 29796.0, io = 201.0, net = 4.75, id = 109 |
| HitCache:true                                                                                                                                                                                                                                                                                                    |
| Source:PLAN_CACHE                                                                                                                                                                                                                                                                                                |
| WorkloadType: TP                                                                                                                                                                                                                                                                                                 |
| TemplateId: 5819c807
  • explain analyze语句:相对于explain cost语句,除了显示各个算子基于统计信息估算的代价以外,该语句可以收集真实运行过程中算子输出的rowCount等信息。
mysql> explain ANALYZE  select  count(*) from lineitem group by L_ORDERKEY;
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| LOGICAL EXECUTIONPLAN                                                                                                                                                                                                                                                                                                                                                                                    |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Project(count(*)="count(*)"): rowcount = 2508.0, cumulative cost = value = 2.4867663E7, cpu = 112574.0, memory = 88984.0, io = 201.0, net = 4.75, actual time = 0.001 + 0.000, actual rowcount = 2506, actual memory = 0, instances = 1, id = 182                                                                                                                                                        |
|   HashAgg(group="L_ORDERKEY", count(*)="SUM(count(*))"): rowcount = 2508.0, cumulative cost = value = 2.4867662E7, cpu = 112573.0, memory = 88984.0, io = 201.0, net = 4.75, actual time = 0.000 + 0.000, actual rowcount = 2506, actual memory = 0, instances = 1, id = 180                                                                                                                             |
|     Gather(concurrent=true): rowcount = 2508.0, cumulative cost = value = 2.4860069E7, cpu = 105039.0, memory = 29796.0, io = 201.0, net = 4.75, actual time = 0.000 + 0.000, actual rowcount = 0, actual memory = 0, instances = 0, id = 178                                                                                                                                                            |
|       LogicalView(tables="[000000-000003].lineitem_[00-15]", shardCount=16, sql="SELECT `L_ORDERKEY`, COUNT(*) AS `count(*)` FROM `lineitem` AS `lineitem` GROUP BY `L_ORDERKEY`"): rowcount = 2508.0, cumulative cost = value = 2.4860068E7, cpu = 105038.0, memory = 29796.0, io = 201.0, net = 4.75, actual time = 0.030 + 0.025, actual rowcount = 10000, actual memory = 0, instances = 0, id = 109 |
| HitCache:true                                                                                                                                                                                                                                                                                                                                                                                            |
| Source:PLAN_CACHE                                                                                                                                                                                                                                                                                                                                                                                        |
| TemplateId: 5819c807                                                                                                                                                                                                                                                                                                                                                                                     |
+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
7 rows in set (1.08 sec)
  • explain physical语句:展示查询在运行过程中执行模式、各个执行片段(Fragment)的依赖关系和并行度。该查询被识别为单机单线程计划模式(TP_LOCAL),执行计划被分为三个片段Fragment-0、Fragment-1和Fragment-2,先做预聚合再做最终的聚合计算,每个片段的执行度可以不同。
mysql> explain physical   select  count(*) from lineitem group by L_ORDERKEY;
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| PLAN                                                                                                                                                                           |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| ExecutorMode: TP_LOCAL                                                                                                                                                         |
| Fragment 0 dependency: [] parallelism: 4                                                                                                                                       |
| Gather(concurrent=true)                                                                                                                                                        |
|   LogicalView(tables="[000000-000003].lineitem_[00-15]", shardCount=16, sql="SELECT `L_ORDERKEY`, COUNT(*) AS `count(*)` FROM `lineitem` AS `lineitem` GROUP BY `L_ORDERKEY`") |
| Fragment 1 dependency: [] parallelism: 8                                                                                                                                       |
| LocalBuffer                                                                                                                                                                    |
|   RemoteSource(sourceFragmentIds=[0], type=RecordType(INTEGER L_ORDERKEY, BIGINT count(*)))                                                                                    |
| Fragment 2 dependency: [0, 1] parallelism: 8                                                                                                                                   |
| Project(count(*)="count(*)")                                                                                                                                                   |
|   HashAgg(group="L_ORDERKEY", count(*)="SUM(count(*))")                                                                                                                        |
|     RemoteSource(sourceFragmentIds=[1], type=RecordType(INTEGER L_ORDERKEY, BIGINT count(*)))                                                                                  |
+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
11 rows in set (0.10 sec)
  • explain advisor语句:主要是会基于统计信息,分析当前查询的执行计划,给用户推荐可以加速查询的全局二级索引。
mysql> explain advisor   select  count(*) from lineitem group by L_ORDERKEY \G;
*************************** 1. row ***************************
IMPROVE_VALUE: 4.4%
  IMPROVE_CPU: 340.8%
  IMPROVE_MEM: 0.0%
   IMPROVE_IO: 1910.0%
  IMPROVE_NET: 0.0%
 BEFORE_VALUE: 2.48676627E7
   BEFORE_CPU: 112573.7
   BEFORE_MEM: 88983.8
    BEFORE_IO: 201
   BEFORE_NET: 4.7
  AFTER_VALUE: 2.38256249E7
    AFTER_CPU: 25536
    AFTER_MEM: 88983.8
     AFTER_IO: 10
    AFTER_NET: 4.7
 ADVISE_INDEX: ALTER TABLE `ads`.`lineitem` ADD GLOBAL INDEX `__advise_index_gsi_lineitem_L_ORDERKEY`(`L_ORDERKEY`) DBPARTITION BY HASH(`L_ORDERKEY`) TBPARTITION BY HASH(`L_ORDERKEY`) TBPARTITIONS 4;
     NEW_PLAN:
Project(count(*)="count(*)")
  HashAgg(group="L_ORDERKEY", count(*)="SUM(count(*))")
    Gather(concurrent=true)
      IndexScan(tables="[000000-000003].lineitem__what_if_gsi_L_ORDERKEY_[00-15]", shardCount=16, sql="SELECT `L_ORDERKEY`, COUNT(*) AS `count(*)` FROM `lineitem__what_if_gsi_L_ORDERKEY` AS `lineitem__what_if_gsi_L_ORDERKEY` GROUP BY `L_ORDERKEY`")
         INFO: GLOBAL_INDEX
1 row in set (0.13 sec)
相关实践学习
快速体验PolarDB开源数据库
本实验环境已内置PostgreSQL数据库以及PolarDB开源数据库:PolarDB PostgreSQL版和PolarDB分布式版,支持一键拉起使用,方便各位开发者学习使用。
相关文章
|
SQL 关系型数据库 MySQL
开发指南—DAL语句—EXPLAIN
该语句用于解释SQL语句的执行计划,包括SELECT、DELETE、INSERT、REPLACE或UPDATE语句。
|
SQL 索引
开发指南—DAL语句—SETCHECK TABLE
本文介绍了CHECK TABLE语句的用法。
|
关系型数据库 MySQL 数据库
开发指南—DAL语句—USE
本文将介绍如何通过USE指令在PolarDB-X中切换当前连接的默认数据库。
107 0
|
SQL 自然语言处理 关系型数据库
开发指南—DQL语句—SELECT
SELECT用于从一个或多个表中查询数据。
|
4天前
|
存储 人工智能 弹性计算
阿里云弹性计算_加速计算专场精华概览 | 2024云栖大会回顾
2024年9月19-21日,2024云栖大会在杭州云栖小镇举行,阿里云智能集团资深技术专家、异构计算产品技术负责人王超等多位产品、技术专家,共同带来了题为《AI Infra的前沿技术与应用实践》的专场session。本次专场重点介绍了阿里云AI Infra 产品架构与技术能力,及用户如何使用阿里云灵骏产品进行AI大模型开发、训练和应用。围绕当下大模型训练和推理的技术难点,专家们分享了如何在阿里云上实现稳定、高效、经济的大模型训练,并通过多个客户案例展示了云上大模型训练的显著优势。
|
8天前
|
存储 人工智能 调度
阿里云吴结生:高性能计算持续创新,响应数据+AI时代的多元化负载需求
在数字化转型的大潮中,每家公司都在积极探索如何利用数据驱动业务增长,而AI技术的快速发展更是加速了这一进程。
|
4天前
|
人工智能 运维 双11
2024阿里云双十一云资源购买指南(纯客观,无广)
2024年双十一,阿里云推出多项重磅优惠,特别针对新迁入云的企业和初创公司提供丰厚补贴。其中,36元一年的轻量应用服务器、1.95元/小时的16核60GB A10卡以及1元购域名等产品尤为值得关注。这些产品不仅价格亲民,还提供了丰富的功能和服务,非常适合个人开发者、学生及中小企业快速上手和部署应用。
|
13天前
|
人工智能 弹性计算 文字识别
基于阿里云文档智能和RAG快速构建企业"第二大脑"
在数字化转型的背景下,企业面临海量文档管理的挑战。传统的文档管理方式效率低下,难以满足业务需求。阿里云推出的文档智能(Document Mind)与检索增强生成(RAG)技术,通过自动化解析和智能检索,极大地提升了文档管理的效率和信息利用的价值。本文介绍了如何利用阿里云的解决方案,快速构建企业专属的“第二大脑”,助力企业在竞争中占据优势。
|
15天前
|
自然语言处理 数据可视化 前端开发
从数据提取到管理:合合信息的智能文档处理全方位解析【合合信息智能文档处理百宝箱】
合合信息的智能文档处理“百宝箱”涵盖文档解析、向量化模型、测评工具等,解决了复杂文档解析、大模型问答幻觉、文档解析效果评估、知识库搭建、多语言文档翻译等问题。通过可视化解析工具 TextIn ParseX、向量化模型 acge-embedding 和文档解析测评工具 markdown_tester,百宝箱提升了文档处理的效率和精确度,适用于多种文档格式和语言环境,助力企业实现高效的信息管理和业务支持。
3936 2
从数据提取到管理:合合信息的智能文档处理全方位解析【合合信息智能文档处理百宝箱】
|
4天前
|
算法 安全 网络安全
阿里云SSL证书双11精选,WoSign SSL国产证书优惠
2024阿里云11.11金秋云创季活动火热进行中,活动月期间(2024年11月01日至11月30日)通过折扣、叠加优惠券等多种方式,阿里云WoSign SSL证书实现优惠价格新低,DV SSL证书220元/年起,助力中小企业轻松实现HTTPS加密,保障数据传输安全。
499 3
阿里云SSL证书双11精选,WoSign SSL国产证书优惠
下一篇
无影云桌面