Teradata 說明

2018-01-12 14:50 更新

EXPLAIN命令以英語返回解析引擎的執(zhí)行計劃。 它可以與除另一個EXPLAIN命令之外的任何SQL語句一起使用。 當(dāng)查詢前面有EXPLAIN命令時,解析引擎的執(zhí)行計劃將返回給用戶,而不是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)

當(dāng)在SELECT語句中沒有指定條件時,優(yōu)化器可以選擇在訪問表的每一行時使用全表掃描。

以下是一個示例查詢,其中優(yōu)化程序可以選擇FTS。

EXPLAIN SELECT * FROM employee;

當(dāng)執(zhí)行上述查詢時,它將產(chǎn)生以下輸出。 可以看出,優(yōu)化器選擇訪問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.

唯一主索引

當(dāng)使用唯一主索引訪問行時,則它是一個AMP操作。

EXPLAIN SELECT * FROM employee WHERE EmployeeNo = 101;

當(dāng)執(zhí)行上述查詢時,它將產(chǎn)生以下輸出。 可以看出,它是單AMP檢索,優(yōu)化器使用唯一的主索引訪問該行。

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.

唯一二級索引

當(dāng)使用唯一二級索引訪問行時,它是一個雙放大操作。

考慮具有以下定義的薪水表。

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;

當(dāng)執(zhí)行上述查詢時,它將產(chǎn)生以下輸出。 可以看出,優(yōu)化器在使用唯一二級索引的兩個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計劃中常見的術(shù)語列表。
... (Last Use) …
不再需要假脫機文件,并且將在此步驟完成后釋放。
... with no residual conditions …
所有適用的條件已應(yīng)用于行。
... END TRANSACTION …
將釋放事務(wù)鎖,并提交更改。
... eliminating duplicate rows ...
重復(fù)行僅存在于假脫機文件中,而不是設(shè)置表。執(zhí)行DISTINCT操作。
... by way of a traversal of index #n extracting row ids only …
構(gòu)建了一個假脫機文件,其中包含在輔助索引(索引#n)中找到的行標(biāo)識
... we do a SMS (set manipulation step) …
使用UNION,MINUS或INTERSECT運算符組合行。
... which is redistributed by hash code to all AMPs.
重新分配數(shù)據(jù)以準(zhǔn)備加入。
... which is duplicated on all AMPs.
在準(zhǔn)備加入時,從較小的表(根據(jù)SPOOL)復(fù)制數(shù)據(jù)。
... (one_AMP) or (group_AMPs)
表示將使用一個AMP或AMP子集而不是所有AMP。
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號