Teradata 说明

2018-01-12 14:50 更新

EXPLAIN命令以英语返回解析引擎的执行计划。 它可以与除另一个EXPLAIN命令之外的任何SQL语句一起使用。 当查询前面有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.

唯一二级索引

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

考虑具有以下定义的薪水表。

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;

当执行上述查询时,它将产生以下输出。 可以看出,优化器在使用唯一二级索引的两个amp操作中检索该行。

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)中找到的行标识
... 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。
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号