teradata:何时使用主索引查询表?

时间:2018-05-06 12:44:32

标签: indexing teradata

我在单个表上创建了一个连接索引,当我用where子句查询表时,一个用" =",另一个用"%",但似乎是第一个查询使用主索引,第二个查询不使用它,不明白,对主索引列的两个查询,为什么第二个查询不使用主索引?

单个表上的连接索引如下:

int havetopay(Client x)
{
   int freeSMS = x.getFreeSMS();
   int sentSMS = x.getSentSMS();
   int SMS;
   if (freeSMS > sentSMS) SMS = 0;
       else SMS = sentSMS - freeSMS;
   return (SMS * x.getSMSPrice() + x.getTalkedMinutes() * x.getMinutesPrice());
}

第一个解释,

CREATE JOIN INDEX CustomerService.EMP_JI AS
SELECT employee_number ,
       department_number,
       employee.last_name,
       manager_employee_number
FROM customerservice.employee
PRIMARY INDEX ( last_name );

第二个解释,

explain sel * from customerservice.employee where last_name = 'tony';


1) First, we do a single-AMP RETRIEVE step from
 CustomerService.EMP_JI by way of the primary index
 "CustomerService.EMP_JI.last_name = 'tony '" with no residual
 conditions into Spool 2 (group_amps), which is redistributed by
 the hash code of (CustomerService.EMP_JI.employee_number) to few
 AMPs.  Then we do a SORT to order Spool 2 by row hash.  The size
 of Spool 2 is estimated with low confidence to be 1 row (45 bytes).
 The estimated time for this step is 0.01 seconds.
 2) Next, we do a group-AMPs JOIN step from customerservice.employee
 by way of a RowHash match scan with no residual conditions, which
 is joined to Spool 2 (Last Use) by way of a RowHash match scan.
 customerservice.employee and Spool 2 are joined using a merge join,
 with a join condition of ("Field_1025 =
 customerservice.employee.employee_number").  The result goes into
 Spool 1 (group_amps), which is built locally on that AMP.  The
 size of Spool 1 is estimated with low confidence to be 1 row (143
 bytes).  The estimated time for this step is 0.03 seconds.
 3) 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.04 seconds.

1 个答案:

答案 0 :(得分:1)

where子句需要具有用于访问主索引的相等条件(=),因为主索引在主索引的散列值上分配行。

Teradata Optimizer Equal vs Like in SQL