7.1 使用执行计划
在进行一个业务SQL的性能调优,或是查看一个业务SQL的计算成本消耗时,使用explain命令来进行查看是一个很好的选择。iDB Cloud for 分析型数据库中提供图形化的执行计划命令,同时,用户也可以手动使用explain指令进行查看。
explain 语句
分析型数据库支持通过explain语句来查看逻辑计划和物理执行计划,当用户发起一个explain查询到分析型数据库系统后,分析型数据库会抽样一个数据分区来分析执行计划,并以图形方式展现给用户。
explain语句的格式为, explain + select语句, 例如:
简单sql的explain<PRE prettyprinted? linenums>
-
explain select count(*) from test4dmp.test where id > 0
复杂sql的explain<PRE prettyprinted? linenums>
-
explain
- select student.id, count(*)
- from test4dmp.student
- inner join test4dmp.grade on student.id =grade.sid
- inner join test4dmp.elective on elective.id=student.id
- group by student.id
- having count(*) > 2
- order by student.id
- limit 10
返回文本格式
当用户通过查询的方式,想要获取文本格式的explain语句后,将会得到如下的json串:
- 返回的json格式<PRE prettyprinted? linenums>
-
| EXPLAIN |
- | logical json |
- | physical json |
返回格式说明一共会返回2行1列并且列名为EXPLAIN的ResultSet记录。其中第一行为逻辑计划,第二行为物理计划。
JSON格式说明Node 代表着唯一的子节点- LeftNode 代表左子树
- RightNode 代表右子树
- MiddleNode 代表所有的中间子树(多叉执行树), 可以有多个MiddleNode
- 其余key-value对统一在同层级的Node和*Node节点显示
Explain 逻辑计划详细语义
样例sql<PRE prettyprinted? linenums>
-
explain
- select student.id, count(*)
- from test4dmp.student
- inner join test4dmp.grade on student.id =grade.sid
- inner join test4dmp.elective on elective.id=student.id
- group by student.id
- having count(*) > 2
- order by student.id
- limit 10
逻辑计划的explain string<PRE prettyprinted? linenums>
-
{
- "Items": ["student.id", "COUNT(*)"],
- "Name": "SelectNode",
- "Node": {
- "Having": "COUNT(*) > 2",
- "Items": ["student.id"],
- "Name": "GroupNode",
- "Node": {
- "Items": ["student.id"],
- "Name": "OrderByNode",
- "Node": {
- "Name": "JoinNode",
- "LeftNode": {
- "Name": "JoinNode",
- "LeftNode": {
- "Name": "TableNode",
- "Schema": ["ID"],
- "TableName": "STUDENT__1"
- },
- "OnCondition": "student.id = grade.sid",
- "RightNode": {
- "Name": "TableNode",
- "Schema": ["SID"],
- "TableName": "GRADE__9"
- }
- },
- "OnCondition": "elective.id = student.id",
- "RightNode": {
- "Name": "TableNode",
- "Schema": ["ID"],
- "TableName": "ELECTIVE__1"
- }
- }
- }
- },
- "Limit:": "LIMIT 100"
- }
逻辑计划explain string的图形化展示效果:
- 逻辑计划各个节点说明:SelectNode表示这select中最终输出表达式的相关信息, 例如select要输出的表达式集合
- GroupNode表示GroupBy语句的相关信息,例如groupby的列,having的表达式等
- OrderByNode表示OrderBy的列信息,例如列名,顺序等。
- JoinNode表示逻辑Join树的信息,例如join的on条件
- TableNode表示分区表的信息,例如参与计算的列,表名等。
Explain 物理执行计划详细语义
样例sql<PRE prettyprinted? linenums>
-
explain
- select student.id, count(*)
- from test4dmp.student
- inner join test4dmp.grade on student.id =grade.sid
- inner join test4dmp.elective on elective.id=student.id
- group by student.id
- having count(*) > 2
- order by student.id
- limit 10
物理计划的explain string<PRE prettyprinted? linenums>
-
{
- "Name": "JoinExecutor",
- "isDimension": "false",
- "LeftNode": {
- "Name": "TableExecutor",
- "PresortCondition": "null",
- "SecpartCondition": "null",
- "QueryColumns": "ID, BOOLEAN_TEST, LONG_TEST",
- "FilterCondition": "null",
- "ResultRows": 0,
- "PartColumn": "ID",
- "IsDimension": "false",
- "TableName": "TEST__2",
- "QueryCondition": "(ID = 0)"
- },
- "JoinCondition": "TEST__2.ID=TEST__2.ID",
- "RightNode": {
- "Name": "TableExecutor",
- "PresortCondition": "null",
- "SecpartCondition": "null",
- "QueryColumns": "ID, BOOLEAN_TEST",
- "FilterCondition": "null",
- "ResultRows": 0,
- "PartColumn": "ID",
- "IsDimension": "false",
- "TableName": "TEST__2",
- "QueryCondition": "((INT_TEST < 0) AND (ID = 0))"
- },
- "JoinType": "innerJoin",
- "WhereFilter": "(BOOLEAN_TEST = BOOLEAN_TEST)",
- "OnFilter": "null"
- }
物理计划explain string的图形化展示效果:
- 物理计划各个节点说明:JoinExecutor表示Join的节点JoinCondition:join的条件
- JoinType:join的类型,innerJoin, leftJoin, rightJoin, simiJoin等
- OnFilter: on上面的过滤条件
- WhereFilter: where里面的过滤条件(只有leftjoin中on和where的条件才有差异)
- IsDimension:该Join子树中是否含有维度表
TableExecutor表示参与计算的表信息
- FilterCondition: 该表参与过滤计算的表达上(不能下沉索引的表达式)
- IsDimension:该表是否是维度表
- PartColumn:一级分区列
- SecPartColumn:二级分区列
- QueryColumns: 参与计算的列数(下沉索引计算的列不包含在此之内)
- QueryCondition: 索引条件
- ResultRows: 该节点预估cost
- PresortCondition: 预排序条件,可以用做优先索引条件下沉
- SecpartCondition:二级分区筛选条件,可以用做二级分区筛选。
- TableName: 分区表名