📜  Teradata-解释

📅  最后修改于: 2020-11-29 09:04:38             🧑  作者: Mango


EXPLAIN命令以英语返回解析引擎的执行计划。它可以与任何SQL语句一起使用,除非在另一个EXPLAIN命令上。当查询前面带有EXPLAIN命令时,解析引擎的执行计划将返回给用户而不是AMP。

范例说明

考虑具有以下定义的表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计划中常见的术语列表。

…(最后使用)…

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

…没有残留条件…

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

…结束交易…

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

…消除重复的行…

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

…通过遍历索引#n的方式仅提取行ID…

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

…我们执行SMS(设置操作步骤)…

使用UNION,MINUS或INTERSECT运算符组合行。

…通过哈希码重新分配给所有AMP。

重新分配数据以准备加入。

…在所有AMP上都是重复的。

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

…(one_AMP)或(group_AMPs)

表示将使用一个AMP或AMP的子集代替所有AMP。