需要优化Teradata查询

时间:2014-02-07 16:34:27

标签: query-optimization teradata

我正在尝试优化以下teradata查询。任何人都可以请这个。检索记录需要花费大量时间。

   select top 100 I.item_sku_nbr,L.loc_nbr,MIS.MVNDR_PRTY_ID from 
QA_US_MASTER_VIEWS.item I,
qa4_US_MASTER_VIEWS.location L,
qa4_US_MASTER_VIEWS.item_str IST,
qa4_US_MASTER_VIEWS.mvndr_item_str MIS 
    where  MIS.str_LOC_ID = L.loc_id and    
mis.str_loc_id = IST.str_loc_id and     
IST.str_loc_id = L.loc_id and   
MIS.ITEM_STAT_CD = IST.ITEM_STAT_CD and     
IST.ITEM_ID = I.ITEM_ID and     
MIS.ITEM_ID = IST.ITEM_ID       and     
I.ITEM_STAT_CD =  100           and 
IST.curr_rmeth_cd = 2           and
MIS.curr_dsvc_typ_cd = 3        and 
MIS.OK_TO_ORD_FLG = 'Y'  and        
MIS.EFF_END_DT = DATE '9999-12-31' and  
IST.EFF_END_DT = DATE '9999-12-31' and 
MIS.ACTV_FLG ='Y' and       
IST.ACTV_FLG ='Y'  and  I.ACTV_FLG='Y'  

解释 QA_US_MASTER.LOCATION 的计划:

1)首先,我们在视图中锁定QA_US_MASTER.LOCATION      qa4_US_MASTER_VIEWS.Location用于访问。

2)接下来,我们从QA_US_MASTER.LOCATION进行全AMPs RETRIEVE步骤      在视图qa4_US_MASTER_VIEWS.Location中通过全行扫描      没有剩余条件进入假脱机1(group_amps),这是      在AMP上本地构建。假脱机1的大小估计为      高信心为10,903行(1,613,644字节)。该      该步骤的估计时间为0.01秒。

3)最后,我们向所有涉及的AMP发送END TRANSACTION步骤      处理请求。

- >作为结果,假脱机1的内容被发送回用户      声明1.估计总时间为0.01秒。

解释 qa4_US_MASTER_VIEWS.item_str 的计划:

1)首先,我们在视图中锁定QA_US_MASTER.item_str      qa4_US_MASTER_VIEWS.item_str用于访问。

2)接下来,我们从QA_US_MASTER.item_str执行全AMPs RETRIEVE步骤      在视图qa4_US_MASTER_VIEWS.item_str中通过全行扫描      没有剩余条件进入假脱机1(group_amps),这是      在AMP上本地构建。输入表将不会被缓存      内存,但它有资格进行同步扫描。结果      假脱机文件不会缓存在内存中。线轴1的大小是      低信心估计为1,229,047,917行(      325,697,698,005字节)。此步骤的预计时间为4      分51秒。

3)最后,我们向所有涉及的AMP发送END TRANSACTION步骤      处理请求。

- >作为结果,假脱机1的内容被发送回用户      声明1.总估计时间为4分51秒。

解释 QA_US_MASTER.ITEM 的计划:

1)首先,我们在视图qa4_US_MASTER_VIEWS.item中锁定QA_US_MASTER.ITEM      进入。

2)接下来,我们从QA_US_MASTER.ITEM进行全AMPs RETRIEVE步骤      通过没有的全行扫描查看qa4_US_MASTER_VIEWS.item      Spool 1(group_amps)中的剩余条件,已构建      在AMP上本地。假脱机1的大小估计很高      信心为1,413,284行(357,560,852字节)。该      此步骤的估计时间为0.40秒。

3)最后,我们向所有涉及的AMP发送END TRANSACTION步骤      处理请求。

- >作为结果,假脱机1的内容被发送回用户      声明1.估计总时间为0.40秒。

解释 QA_US_MASTER.MVNDR_ITEM_STR的计划:

1)首先,我们在视图中锁定QA_US_MASTER.MVNDR_ITEM_STR      qa4_US_MASTER_VIEWS.mvndr_item_str用于访问。

2)接下来,我们从中进行全AMPs RETRIEVE步骤      QA_US_MASTER.MVNDR_ITEM_STR在视图中      qa4_US_MASTER_VIEWS.mvndr_item_str通过全行扫描      没有剩余条件进入假脱机1(group_amps),这是建立的      在AMP上本地。输入表不会缓存在内存中,      但它有资格进行同步扫描。结果假脱机      文件不会缓存在内存中。线轴1的大小是      高信心估计为1,316,279,746行(      327,753,656,754字节)。此步骤的估计时间为6      分4秒。

3)最后,我们向所有涉及的AMP发送END TRANSACTION步骤      处理请求。

- >作为结果,假脱机1的内容被发送回用户      声明1.总估计时间为6分4秒。

解释整个查询的计划:

  1) First, we lock QA_US_MASTER.ITEM in view QA_US_MASTER_VIEWS.item
  for access, we lock QA_US_MASTER.LOCATION in view
  qa4_US_MASTER_VIEWS.location for access, we lock
  QA_US_MASTER.MVNDR_ITEM_STR in view
  qa4_US_MASTER_VIEWS.mvndr_item_str for access, and we lock
  QA_US_MASTER.item_str in view qa4_US_MASTER_VIEWS.item_str for
  access. 

  2) Next, we execute the following steps in parallel. 
   1) We do an all-AMPs RETRIEVE step from QA_US_MASTER.LOCATION in
      view qa4_US_MASTER_VIEWS.location by way of an all-rows scan
      with no residual conditions into Spool 3 (all_amps)
      (compressed columns allowed), which is duplicated on all AMPs. 
      The size of Spool 3 is estimated with high confidence to be
      1,013,979 rows (20,279,580 bytes).  The estimated time for
      this step is 0.03 seconds. 

    2) We do an all-AMPs RETRIEVE step from QA_US_MASTER.ITEM in
      view QA_US_MASTER_VIEWS.item by way of an all-rows scan with
      a condition of ("(QA_US_MASTER.ITEM in view
      QA_US_MASTER_VIEWS.item.ITEM_STAT_CD = 100) AND
      (QA_US_MASTER.ITEM in view QA_US_MASTER_VIEWS.item.ACTV_FLG =
      'Y')") into Spool 4 (all_amps) (compressed columns allowed)
      fanned out into 14 hash join partitions, which is duplicated
      on all AMPs.  The size of Spool 4 is estimated with low
      confidence to be 30,819,363 rows (678,025,986 bytes).  The
      estimated time for this step is 0.81 seconds. 

     3) We do an all-AMPs JOIN step from Spool 3 (Last Use) by way of an
    all-rows scan, which is joined to QA_US_MASTER.item_str in view
    qa4_US_MASTER_VIEWS.item_str by way of an all-rows scan with a
    condition of 
    ("(QA_US_MASTER.item_str in view
    qa4_US_MASTER_VIEWS.item_str.CURR_RMETH_CD = 2) AND
     ((QA_US_MASTER.item_str in view
    qa4_US_MASTER_VIEWS.item_str.EFF_END_DT = DATE '9999-12-31') AND
    (QA_US_MASTER.item_str in view
    qa4_US_MASTER_VIEWS.item_str.ACTV_FLG = 'Y'))").  Spool 3 and
    QA_US_MASTER.item_str are joined using a dynamic hash join, with a
    join condition of ("QA_US_MASTER.item_str.STR_LOC_ID = LOC_ID"). 
    The input table QA_US_MASTER.item_str will not be cached in memory. 
    The result goes into Spool 5 (all_amps) (compressed columns
    allowed), which is built locally on the AMPs into 14 hash join
    partitions.  The size of Spool 5 is estimated with no confidence
    to be 69,133,946 rows (2,419,688,110 bytes).  The estimated time
    for this step is 1 minute and 8 seconds. 

    4) We do an all-AMPs JOIN step from Spool 4 (Last Use) by way of an
   all-rows scan, which is joined to Spool 5 (Last Use) by way of an
   all-rows scan.  Spool 4 and Spool 5 are joined using a hash join
   of 14 partitions, with a join condition of ("(ITEM_ID = ITEM_ID)
   AND (ACTV_FLG = ACTV_FLG)").  The result goes into Spool 6
   (all_amps) (compressed columns allowed), which is redistributed by
   the hash code of (QA_US_MASTER.item_str.STR_LOC_ID,
   QA_US_MASTER.item_str.ITEM_STAT_CD, QA_US_MASTER.item_str.ITEM_ID,
   QA_US_MASTER.ITEM.ITEM_ID, QA_US_MASTER.LOCATION.LOC_ID) to all
    AMPs into 33 hash join partitions.  The size of Spool 6 is
   estimated with no confidence to be 36,434,893 rows (1,603,135,292
   bytes).  The estimated time for this step is 9.11 seconds. 

    5) We do an all-AMPs RETRIEVE step from QA_US_MASTER.MVNDR_ITEM_STR
   in view qa4_US_MASTER_VIEWS.mvndr_item_str by way of an all-rows
   scan with a condition of ("(QA_US_MASTER.MVNDR_ITEM_STR in view
   qa4_US_MASTER_VIEWS.mvndr_item_str.CURR_DSVC_TYP_CD = 3) AND
    ((QA_US_MASTER.MVNDR_ITEM_STR in view
   qa4_US_MASTER_VIEWS.mvndr_item_str.EFF_END_DT = DATE '9999-12-31')
   AND ((QA_US_MASTER.MVNDR_ITEM_STR in view
   qa4_US_MASTER_VIEWS.mvndr_item_str.ACTV_FLG = 'Y') AND
   (QA_US_MASTER.MVNDR_ITEM_STR in view
   qa4_US_MASTER_VIEWS.mvndr_item_str.OK_TO_ORD_FLG = 'Y')))") into
   Spool 7 (all_amps) (compressed columns allowed) fanned out into 33
   hash join partitions, which is redistributed by the hash code of (
   QA_US_MASTER.MVNDR_ITEM_STR.ITEM_ID,
   QA_US_MASTER.MVNDR_ITEM_STR.STR_LOC_ID,
   QA_US_MASTER.MVNDR_ITEM_STR.ITEM_STAT_CD,
   QA_US_MASTER.MVNDR_ITEM_STR.ITEM_ID,
   QA_US_MASTER.MVNDR_ITEM_STR.STR_LOC_ID) to all AMPs.  The input
    table will not be cached in memory, but it is eligible for
   synchronized scanning.  The size of Spool 7 is estimated with no
   confidence to be 173,967,551 rows (5,914,896,734 bytes).  The
   estimated time for this step is 2 minutes and 23 seconds. 

   6) We do an all-AMPs JOIN step from Spool 6 (Last Use) by way of an
   all-rows scan, which is joined to Spool 7 (Last Use) by way of an
   all-rows scan.  Spool 6 and Spool 7 are joined using a hash join
   of 33 partitions, with a join condition of ("(STR_LOC_ID =
   STR_LOC_ID) AND ((ITEM_STAT_CD = ITEM_STAT_CD) AND ((ITEM_ID =
   ITEM_ID) AND ((ACTV_FLG = OK_TO_ORD_FLG) AND ((ACTV_FLG = ACTV_FLG)
   AND ((EFF_END_DT = EFF_END_DT) AND ((ACTV_FLG = ACTV_FLG) AND
   ((OK_TO_ORD_FLG = ACTV_FLG) AND ((ITEM_ID = ITEM_ID) AND
   (STR_LOC_ID = LOC_ID )))))))))").  The result goes into Spool 2
   (all_amps) (compressed columns allowed), which is built locally on
   the AMPs.  The size of Spool 2 is estimated with no confidence to
   be 12,939,628 rows (336,430,328 bytes).  The estimated time for
   this step is 4.00 seconds. 

   7) We do an all-AMPs STAT FUNCTION step from Spool 2 by way of an
  all-rows scan into Spool 10, which is redistributed by hash code
  to all AMPs.  The result rows are put into Spool 1 (group_amps),
  which is built locally on the AMPs.  This step is used to retrieve
  the TOP 100 rows.  Load distribution optimization is used.
  If this step retrieves less than 100 rows, then execute step 8. 
  The size is estimated with no confidence to be 100 rows (3,200
  bytes). 

  8) We do an all-AMPs STAT FUNCTION step from Spool 2 (Last Use) by
  way of an all-rows scan into Spool 10 (Last Use), which is
  redistributed by hash code to all AMPs.  The result rows are put
   into Spool 1 (group_amps), which is built locally on the AMPs. 
  This step is used to retrieve the TOP 100 rows.  The size is
  estimated with no confidence to be 100 rows (3,200 bytes).

  9) 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. 

1 个答案:

答案 0 :(得分:1)

您的查询中没有ORDER BY,所以您只想要100个随机行?

在Teradata中,在创建完整结果集后,TOP 完成。您应该将TOP移动到派生表中,如:

select I.item_sku_nbr,L.loc_nbr,MIS.MVNDR_PRTY_ID from 
QA_US_MASTER_VIEWS.item I,
qa4_US_MASTER_VIEWS.location L,
(SELECT TOP 100 * FROM qa4_US_MASTER_VIEWS.item_str) IST,
qa4_US_MASTER_VIEWS.mvndr_item_str MIS 
    where  MIS.str_LOC_ID = L.loc_id and    
mis.str_loc_id = IST.str_loc_id and     
IST.str_loc_id = L.loc_id and   
MIS.ITEM_STAT_CD = IST.ITEM_STAT_CD and     
IST.ITEM_ID = I.ITEM_ID and     
MIS.ITEM_ID = IST.ITEM_ID       and     
I.ITEM_STAT_CD =  100           and 
IST.curr_rmeth_cd = 2           and
MIS.curr_dsvc_typ_cd = 3        and 
MIS.OK_TO_ORD_FLG = 'Y'  and        
MIS.EFF_END_DT = DATE '9999-12-31' and  
IST.EFF_END_DT = DATE '9999-12-31' and 
MIS.ACTV_FLG ='Y' and       
IST.ACTV_FLG ='Y'  and  I.ACTV_FLG='Y'