Teradata 简明教程

Teradata - Explain

EXPLAIN 命令以英语返回解析引擎的执行计划。它可以与任何 SQL 语句一起使用,但不能与另一个 EXPLAIN 命令一起使用。当查询以 EXPLAIN 命令开头时,解析引擎的执行计划将返回给用户,而不是 AMP。

Examples of 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 计划示例。

Full Table Scan (FTS)

当 SELECT 语句中未指定条件时,优化器可能会选择使用全表扫描,在该扫描中将访问表中的每一行。

Example

以下是优化器可能选择 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.

Unique Primary Index

当使用唯一的主索引访问行时,则是一个 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.

Unique Secondary Index

当使用唯一辅助索引访问行时,这是一个两安操作。

Example

查看如下定义的表“薪资”。

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.

Additional Terms

以下是 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。