Teradata - Explain

EXPLAIN 命令以英文形式返回解析引擎的执行计划。它可以与任何 SQL 语句一起使用,但不能与其他 EXPLAIN 命令一起使用。当查询前面带有 EXPLAIN 命令时,解析引擎的执行计划将返回给用户,而不是 AMP。

EXPLAIN 示例

考虑具有以下定义的 Employee 表。

CREATE SET TABLE EMPLOYEE,FALLBACK ( 
   EmployeeNo INTEGER, 
   FirstName VARCHAR(30), 
   LastName VARCHAR(30),
   DOB DATE FORMAT 'YYYY-MM-DD', 
   JoinedDate DATE FORMAT 'YYYY-MM-DD', 
   DepartmentNo BYTEINT 
) 
UNIQUE PRIMARY INDEX ( EmployeeNo );

下面给出了 EXPLAIN 计划的一些示例。

全表扫描 (FTS)

如果 SELECT 语句中未指定任何条件,则优化器可能会选择使用全表扫描,其中访问表的每一行。

示例

以下是优化器可能选择 FTS 的示例查询。

EXPLAIN SELECT * FROM employee;

执行上述查询时,会产生以下输出。可以看出,优化器选择访问所有 AMP 和 AMP 内的所有行。

1) First, we lock a distinct TDUSER."pseudo table" for read on a 
   RowHash to prevent global deadlock for TDUSER.employee.  
2) Next, we lock TDUSER.employee for read.  
3) We do an all-AMPs RETRIEVE step from TDUSER.employee by way of an
   all-rows scan with no residual conditions into Spool 1 
   (group_amps), which is built locally on the AMPs.  The size of 
   Spool 1 is estimated with low confidence to be 2 rows (116 bytes).  
   The estimated time for this step is 0.03 seconds.  
4) Finally, we send out an END TRANSACTION step to all AMPs involved 
   in processing the request. 
→ The contents of Spool 1 are sent back to the user as the result of 
   statement 1.  The total estimated time is 0.03 seconds.

唯一主索引

当使用唯一主索引访问行时,这是一个 AMP 操作。

EXPLAIN SELECT * FROM employee WHERE EmployeeNo = 101;

执行上述查询时,会产生以下输出。可以看出,这是一个单 AMP 检索,优化器正在使用唯一主索引来访问行。

1) First, we do a single-AMP RETRIEVE step from TDUSER.employee by 
   way of the unique primary index "TDUSER.employee.EmployeeNo = 101" 
   with no residual conditions. The estimated time for this step is 
   0.01 seconds.  
→ The row is sent directly back to the user as the result of 
   statement 1.  The total estimated time is 0.01 seconds.

唯一二级索引

当使用唯一二级索引访问行时,这是一个双重操作。

示例

考虑具有以下定义的 Salary 表。

CREATE SET TABLE SALARY,FALLBACK ( 
   EmployeeNo INTEGER, 
   Gross INTEGER, 
   Deduction INTEGER, 
   NetPay INTEGER 
)
PRIMARY INDEX ( EmployeeNo ) 
UNIQUE INDEX (EmployeeNo);

考虑以下 SELECT 语句。

EXPLAIN SELECT * FROM Salary WHERE EmployeeNo = 101;

执行上述查询时,会产生以下输出。可以看出,优化器使用唯一二级索引在两次操作中检索行。

1) First, we do a two-AMP RETRIEVE step from TDUSER.Salary 
   by way of unique index # 4 "TDUSER.Salary.EmployeeNo = 
   101" with no residual conditions.  The estimated time for this 
   step is 0.01 seconds.  
→ The row is sent directly back to the user as the result of 
   statement 1.  The total estimated time is 0.01 seconds.

附加术语

以下是 EXPLAIN 计划中常见的术语列表。

... (Last Use) …

不再需要假脱机文件,此步骤完成后将释放该文件。

... with no residual conditions …

所有适用条件均已应用于行。

... END TRANSACTION …

事务锁被释放,更改被提交。

... eliminating duplicate rows ...

重复行仅存在于假脱机文件中,不存在于设置表中。执行 DISTINCT 操作。

... by way of a traversal of index #n extracting row ids only …

构建一个假脱机文件,其中包含在二级索引(索引 #n)中找到的行 ID

... we do a SMS (set manipulation step) …

使用 UNION、MINUS 或 INTERSECT 运算符合并行。

... which is redistributed by hash code to all AMPs.

重新分配数据以准备连接。

... which is duplicated on all AMPs.

从较小的表中复制数据(就 SPOOL 而言)以准备连接。

... (one_AMP) or (group_AMPs)

表示将使用一个 AMP 或 AMP 子集,而不是所有 AMP。